Rails 3 has_many 关系中nested_attributes 的 I18n 标签翻译
使用: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'
,但不适用于 person[wives_attributes]
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我这样做是这样的:
这很好,但当你有无限的选择时并不理想。我只是在表单生成器中指定一个自定义翻译键:)
编辑:
不知道这是否是一个新功能,但似乎这样做就像一个魅力:
I did this with :
Which is nice but not ideal when you have unlimited options.. I would just specify a custom translation key in the form builder :)
EDIT :
Don't know if it's a new feature but seems to work like a charm doing this :
在我的 Rails 3.2.13 应用程序中,属性标签是从嵌入属性的模型中自动选取的。请注意,我嵌套了belongs_to模型的属性,但它也可能以相反的方式工作。
我的工作代码示例:
模型:
视图:
translations_de.yml:
视图是使用基于
Nice 翻译的子域标签呈现的。 :)
我不确定,但它可能需要您使用 activerecord 路径而不是助手路径。
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:
The view:
translations_de.yml:
And the view is rendered with the subdomain label translated based on
Nice. :)
I'm not sure but it might require you to use the activerecord path instead of the helpers one.