2012-02-13 83 views
3

如何在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.nameactiverecord.attributes.user.company_information_name不拾取。

回答

1

看来你正在使用simple_form创业板发电形式。这是对我有用的东西。

en: 
    simple_form: 
    labels: 
     user: 
     company_information: 
      name: My Name 

链接到简单窗体的localization chapter也可能有用。

0

在导轨3我找到了一种方法。你的情况的翻译是helpers.label.user[company_information].name在en.yml它看起来像这样

en: 
    helpers: 
    label: 
     "user[company_information]": 
     name:"Name" 
+0

我试过这个解决方案,虽然看起来完美的形式,如果有一个验证错误,那么这不会被拿起,它会恢复到默认值。 有什么建议吗? – richard 2013-09-19 02:28:33