2012-02-13 29 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