Ruby on Rails,如何使用 i18n 查找嵌套多态?
Ruby on Rails, how to use i18n look up on nested polymorphic?
这些是我的模型
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 中,我试图为员工地址和客户地址显示不同的文本(例如,员工地址显示 "These are the known employee address",客户地址显示 "Clients can be found here") .
这是我的 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)
这将有大量重复的标签,因为 "new" 和 "edit" 以及 "show" 都有相同的标签。
第二种方法是使用作用域,比如
t .address, scope: [:locatable, :new, :address]
这样,我就不需要将地址分成新建、编辑和显示。但是,我不确定如何检索 :locatable 范围。或者有更好的方法吗?
请帮忙!
经过数小时的反复试验,由
解决
en:
:employees:
:address: 'These are the known employee address'
:clients:
:address: 'Clients can be found here'
并在 _form
<%= t("#{params[: locatable]}.address") %>
简单...
这些是我的模型
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 中,我试图为员工地址和客户地址显示不同的文本(例如,员工地址显示 "These are the known employee address",客户地址显示 "Clients can be found here") .
这是我的 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)
这将有大量重复的标签,因为 "new" 和 "edit" 以及 "show" 都有相同的标签。 第二种方法是使用作用域,比如
t .address, scope: [:locatable, :new, :address]
这样,我就不需要将地址分成新建、编辑和显示。但是,我不确定如何检索 :locatable 范围。或者有更好的方法吗?
请帮忙!
经过数小时的反复试验,由
解决en:
:employees:
:address: 'These are the known employee address'
:clients:
:address: 'Clients can be found here'
并在 _form
<%= t("#{params[: locatable]}.address") %>
简单...