Rails 3 I18n label translation for nested_attributes in has_many relationship

后端 未结 2 1124
灰色年华
灰色年华 2021-01-13 18:37

Using: Rails 3.0.3, Ruby 1.9.2

Here\'s the relationship:

class Person < ActiveRecord::Base
  has_many :contact_methods
  accepts_nested_attributes         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-13 19:17

    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'
    

提交回复
热议问题