This project is archived and is in readonly mode.

#6273 open
Paul Schreiber

Cannot create I18n labels when using accepts_nested_attributes_for

Reported by Paul Schreiber | January 10th, 2011 @ 05:16 PM

Note: I am using Rails 2.3.10.

Normally, you can use Rails’ I18n system to generate label text.

For example, suppose you have a Person class with a name attribute. And this ERB:
<%= form_for @person do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<% end %>

And you’d construct your en.yml like so:
en:
helpers:

label:
  name: “your name”

However, this doesn’t work very well with related objects and accepts_nested_attributes_for. Suppose you have the same Person class as before. And person has_many :activities (likewise, activity belongs_to :person) and accepts_nested_attributes_for :activities.

Now your ERB looks like this:
<%= form_for @person do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>

    <% f.fields_for :activities do |a| %>
            <%= l.label :difficulty %> 
            <%= l.text_field :difficulty %> 
    <% end %>

<% end %>

Various combinations indentation of person / activities / difficulty in my en.yml file didn’t work. So I looked inside rails to see what’s going on.

The relevant code is in actionpack-2.3.10/lib/action_view/helpers/form_helper.rb. The method used is

    def to_label_tag(text = nil, options = {})

on line 758.

And the code doing the work is:

    content = if text.blank?
      i18n_label = I18n.t("helpers.label.#{object_name}.#{method_name}", :default => “”) 
      i18n_label if i18n_label.present? 
    else 
      text.to_s 
    end

The problem is you end up with a set of labels like:

    helpers.label.person[activities_attributes][0].difficulty
    helpers.label.person[activities_attributes][1].difficulty 
    helpers.label.person[activities_attributes][2].difficulty

Comments and changes to this ticket

  • Alexey Nayden

    Alexey Nayden January 14th, 2011 @ 12:13 AM

    • Tag changed from i18n label, accepts_nested_attributes_for, i18n, label to i18n label, 2.3.10, accepts_nested_attributes_for, actionpack, i18n, label
  • Alexey Nayden

    Alexey Nayden January 23rd, 2011 @ 11:53 PM

    • Tag changed from i18n label, 2.3.10, accepts_nested_attributes_for, actionpack, i18n, label to i18n label, 2.3.10, accepts_nested_attributes_for, actionpack, actionview, i18n, label, patch, rails3.0.3
    • Assigned user set to “Jeremy Kemper”

    The same problem found in rails 3.0.3

    Here're test and patch

  • Alexey Nayden
  • Alexey Nayden

    Alexey Nayden January 29th, 2011 @ 02:56 AM

    • Assigned user changed from “Jeremy Kemper” to “Aaron Patterson”
  • rails

    rails April 30th, 2011 @ 01:00 AM

    • State changed from “new” to “open”
    • Tag changed from i18n label, 2.3.10, accepts_nested_attributes_for, actionpack, actionview, i18n, label, patch, rails3.0.3 to i18n label, 2310, accepts_nested_attributes_for, actionpack, actionview, i18n, label, patch, rails303

    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.

  • rails

    rails April 30th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”
  • Paul Schreiber

    Paul Schreiber April 30th, 2011 @ 01:04 AM

    • State changed from “stale” to “open”

    [state:open]

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