Rails 3 I18n label translation for nested_attributes in has_many relationship

…衆ロ難τιáo~ 提交于 2019-12-30 09:53:27

问题


Using: Rails 3.0.3, Ruby 1.9.2

Here's the relationship:

class Person < ActiveRecord::Base
  has_many :contact_methods
  accepts_nested_attributes_for :contact_methods
end

class ContactMethod < ActiveRecord::Base
  attr_accessible :info
  belongs_to :person
end

Now when I try to customize the contact_method labels in I18n, it doesn't recognize it.

en:
  helpers:
    label:
      person[contact_methods_attributes]: 
        info: 'Custom label here'

I have also tried:

person[contact_method_attributes]

This works just fine for 1-1 relationships, e.g.

person[wife_attributes]: 
  name: 'My wife'

but not person[wives_attributes]

Thanks in advance


回答1:


I did this with :

en:
  helpers:
    label:
      person[contact_methods_attributes][0]: 
        info: 'First custom label here'
      person[contact_methods_attributes][1]: 
        info: 'Second custom label here'

Which is nice but not ideal when you have unlimited options.. I would just specify a custom translation key in the form builder :)

en:
  helpers:
    label:
      person[contact_methods_attributes][any]: 
        info: 'Custom label here'

<% fields_for :contact_methods do |builder| %>
  <%= builder.label :info, t("helpers.person[contact_methods_attributes][any].info") %>
  <%= builder.text_field :info %>
<% end %>

EDIT : Don't know if it's a new feature but seems to work like a charm doing this :

en:
  helpers:
    label:
      person:
        contact_methods: 
          info: 'Custom label here'



回答2:


In my Rails 3.2.13 app the attribute labels are picked up automatically from the model whose attributes are embedded. Please note that I am nesting attributes of the belongs_to model, but it might also work the other way around.

My example from working code:

The models:

class User < ActiveRecord::Base
  belongs_to :account
  accepts_nested_attributes_for :account
  # ...
end

class Account < ActiveRecord::Base
  has_many :users
end

The view:

<h2><%= t(:sign_up) %></h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <%= f.fields_for :account do |account_form| %>

    <div><%= account_form.label :subdomain %><br />
    <%= account_form.text_field :subdomain %>.<%= request.host %> <span class="hint"></span></div>

  <% end %>

translations_de.yml:

activerecord:

 models:
  account: Konto
  user: Benutzer

 attributes:
  account:
    name: Firmenname
    subdomain: Subdomain
    users: Benutzer

  user:
    # ... no sign of subdomain here ...

And the view is rendered with the subdomain label translated based on

activerecord.attributes.account.subdomain

Nice. :)

I'm not sure but it might require you to use the activerecord path instead of the helpers one.



来源:https://stackoverflow.com/questions/4479950/rails-3-i18n-label-translation-for-nested-attributes-in-has-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!