This project is archived and is in readonly mode.

#5665 ✓stale
Anton Bangratz

Label form helper and I18n with nested attributes does not work

Reported by Anton Bangratz | September 20th, 2010 @ 08:31 AM

Using rails 3, ruby 1.9.2, I can verify the following behaviour:

When using the shortcut label :fieldname in the context of nested attributes while having the corresponding attributes in the 'helpers.label.model.attribute' namespace does not render the I18n text, because the object name looks like 'parent[child_attributes]'.

Example:

Given the translation 'some.helpers.label.author.name' set to 'Author Name' and the locale set to 'some', the following code

form_for(@post) do
  fields_for(:author) do
    label :name
  end
end

will render

...
<label for="post_author_attributes_name">Name</label>
...

because the current lookup of the nested (child) class is not reaching the defined I18n string, and thus calls 'name'.humanize as a fall back.

My small patch detects this nested attribute and sets the I18n lookup correctly, . It is a crude patch using assumptions and a regular expression, and could maybe done better. Thoughts?

Patch also applies to 3.0.0 installed via bundler, failing at tests.

Comments and changes to this ticket

  • Tsutomu Kuroda

    Tsutomu Kuroda October 1st, 2010 @ 02:22 PM

    Hi, Anton

    The object_name may have plural form before '_attributes'.

    You should singularize the base_name like:

      base_name = $1.singularize
    

    But, I propose the following modification:

      base_name = object.class.name.underscore
    

    because the object name can be different from the model name.

  • Anton Bangratz

    Anton Bangratz October 1st, 2010 @ 05:34 PM

    Thank you,

    I have added the suggestions. The tests suggest that the method works - finding where the object comes from was a bit ominous at first glance.

    Still, I have two separate patches to implement either solution - anyone will have to apply them in order.

    Anton

  • Tsutomu Kuroda

    Tsutomu Kuroda October 8th, 2010 @ 08:15 PM

    Anton,

    I changed my mind about this theme. Now I think that the current implementation is correct.

    Here is the relevant code snipet from form_helper.rb:

      content = if text.blank?
        I18n.t("helpers.label.#{object_name}.#{method_name}", :default => "").presence
      else
        text.to_s
      end
    
      content ||= if object && object.class.respond_to?(:human_attribute_name)
        object.class.human_attribute_name(method_name)
      end
    
      content ||= method_name.humanize
    

    The class method human_attribute_name looks into the lang.activerecord.attributes.author namespace. The YAML file will go as follow:

    ja:
      activerecord:
        attributes:
          author:
            name: 名前
    

    This is the normal and traditional way to translate the label text.

    Alternatively, we can provide texts in the lang.helpers.label.post[author_attributes] namespace like this:

    ja:
      helpers:
        label:
          post[author_attributes]:
            name: 投稿者名
    

    We might want to provide other texts for the attributes of the Author model nested in the another model, say Article. In this case, we will modify the YAML file as follows:

    ja:
      helpers:
        label:
          post[author_attributes]:
            name: 投稿者名
          article[author_attributes]:
            name: 筆者名
    

    I am happy with this behaviour.

  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:22 AM

    • Tag cleared.
    • Importance changed from “” to “Low”

    Automatic cleanup of spam.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:29 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:29 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages