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
Is there a way you can put wildcards in YAML? If not, is there some other way
around this limitation? If not, this seems like a bug in Rails, and I’ll file a
lighthouse ticket.
Paul
Garrett Lancaster
2010-Dec-20 17:24 UTC
Re: Labels, I18n and accepts_nested_attributes_for
I am having the same problem with Rails 3.0.3. If you don''t require context-dependent labels, you can use en.activerecord.attributes.activity.some_field_here (notice it is not nested inside the person model). However, I have not found any workaround if your label IS context dependent, and I believe it should probably be filed on lighthouse. Garrett On Nov 28, 9:17 pm, Paul Schreiber <paulschrei...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > Is there a way you can put wildcards in YAML? If not, is there some other way around this limitation? If not, this seems like a bug in Rails, and I’ll file a lighthouse ticket. > > Paul > > smime.p7s > 4KViewDownload-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 20 2010, 12:24 pm, Garrett Lancaster <glanc...-jmyiO2ngOJdad6c/EObmYVaTQe2KTcn/@public.gmane.org> wrote:> I am having the same problem with Rails 3.0.3. If you don''t require > context-dependent labels, you can use > en.activerecord.attributes.activity.some_field_here (notice it is not > nested inside the person model). > However, I have not found any workaround if your label IS context > dependent, and I believe it should probably be filed on lighthouse.Filed as: <https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/ 6273-cannot-create-i18n-labels-when-using- accepts_nested_attributes_for> Paul -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.