加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

国际化 – 在has_many关系中针对nested_attributes的Rails 3 I18

发布时间:2020-12-17 02:28:03 所属栏目:百科 来源:网络整理
导读:使用:Rails 3.0.3,Ruby 1.9.2 这是关系: class Person ActiveRecord::Base has_many :contact_methods accepts_nested_attributes_for :contact_methodsendclass ContactMethod ActiveRecord::Base attr_accessible :info belongs_to :personend 现在,当我
使用:Rails 3.0.3,Ruby 1.9.2

这是关系:

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

现在,当我尝试在I18n中自定义contact_method标签时,它无法识别它.

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

我也尝试过:

person[contact_method_attributes]

这适用于1-1关系,例如

person[wife_attributes]: 
  name: 'My wife'

但不是人[wives_attributes]

提前致谢

解决方法

我这样做:

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

当你有无限的选项时哪个好但不理想..我只想在表单生成器中指定一个自定义翻译键:)

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 %>

编辑:
不知道它是否是一个新功能,但似乎像一个魅力这样做:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读