在 Rails 中本地化嵌套虚拟属性
如何在 Rails 中本地化嵌套虚拟属性?
模型:
class User < ActiveRecord::Base
attr_accessor :company_information # This is used in callbacks etc
end
和视图:
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: {class: 'form-horizontal'}) do |f|
= devise_error_messages!
= f.input :email
= f.input :password
= f.input :password_confirmation
= f.simple_fields_for :company_information do |c|
= c.input :name # This is what I want to localise
= f.button :submit
翻译键(来自 en.yml
),例如 activerecord.attributes.user.company_information.name
和 activerecord.attributes.user。未选取company_information_name
。
How is it possible to localise nested virtual attribute in Rails?
The model:
class User < ActiveRecord::Base
attr_accessor :company_information # This is used in callbacks etc
end
and the view:
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: {class: 'form-horizontal'}) do |f|
= devise_error_messages!
= f.input :email
= f.input :password
= f.input :password_confirmation
= f.simple_fields_for :company_information do |c|
= c.input :name # This is what I want to localise
= f.button :submit
The translation keys (from en.yml
) like activerecord.attributes.user.company_information.name
and activerecord.attributes.user.company_information_name
aren't picked up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您正在使用 simple_form gem 来生成表单。这对我有用。
简单表单的本地化章节的链接也可能很有用。
It seems you are using simple_form gem for generation of forms. Here is what worked for me.
The link to simple form's localization chapter may be also useful.
在 Rails 3 中我找到了一种方法。在您的情况下,翻译将是 en.yml 中的
helpers.label.user[company_information].name
,它看起来像这样In rails 3 i found one way. In your case translation would be
helpers.label.user[company_information].name
in en.yml it would look like this