2015-06-23 17 views
1

这些都是我的模型Ruby on Rails,如何使用i18n查看嵌套多态?

class Employee < ActiveRecord::Base 
    has_many addresses, as: :locatable 
end 

class Client < ActiveRecord::Base 
    has_many addresses, as: :locatable 
end 

class Address < ActiveRecord::Base 
    belongs_to :locatable, polymorphic: true 
end 

我把我的路线是

resources :employees do 
    resources :addresses, :defaults => { :locatable => 'employee'} 
end 

resources :clients do 
    resources :addresses, :defaults => { :locatable => 'clients'} 
end 

于地址_form.html.erb,我试图表现出不同的文字作为员工地址和客户解决(例如员工地址显示“这些是已知的员工地址”,客户地址显示“客户可以在此找到”)。

这是我的i18n文件

en: 
    employees: 
    new: 
     address: 'These are the known employee address' 
    edit: 
     address: 'These are the known employee address' 
    show: 
     address: 'These are the known employee address' 
    clients: 
    new: 
     address: 'Clients can be found here' 
    edit: 
     address: 'Clients can be found here' 
    show: 
     address: 'Clients can be found here' 

我能想到的2种方法。 第一种是使用延迟加载

t(.address) 

这将有反复标签的负荷,因为“新”,“编辑”和“秀”都具有相同的标签。 第2方法是使用范围的,像

t .address, scope: [:locatable, :new, :address] 

这样,我就不用再地址分成新,编辑和展示。但是,我不知道如何检索:可定位的作用域。还是有更好的方法呢?

请帮忙!

回答

1

后试错的时间,通过

en: 
    :employees: 
    :address: 'These are the known employee address' 
    :clients: 
    :address: 'Clients can be found here' 

而在_form

<%= t("#{params[: locatable]}.address") %> 

简单的解决...