这些是我的模特
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在address _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'我能想到的两种方法。第一种方法是使用延迟加载。
t(.address)这将有大量重复标签,因为“新”和“编辑”和“显示”都有相同的标签。第二种方法是使用作用域,类似
t .address, scope: [:locatable, :new, :address] 这样,我就不需要将地址分离成新地址、编辑地址和显示地址。但是,我不确定如何检索:可定位范围。还是有更好的方法来解决这个问题?
请帮帮我!
发布于 2015-06-23 07:00:30
经过几个小时的试验和错误,通过
en:
:employees:
:address: 'These are the known employee address'
:clients:
:address: 'Clients can be found here'在_form中
<%= t("#{params[: locatable]}.address") %>简单..。
https://stackoverflow.com/questions/30994828
复制相似问题