Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Label form helper and I18n with nested attributes does not work

#5665

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.

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

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Tsutomu Kuroda
    Tsutomu Kuroda

    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.

    October 1st, 2010 @ 02:22 PM

  2. Anton Bangratz
    Anton Bangratz

    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

    October 1st, 2010 @ 05:34 PM

  3. Tsutomu Kuroda
    Tsutomu Kuroda

    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.

    October 8th, 2010 @ 08:15 PM

  4. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:22 AM

  5. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:29 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:29 PM