我更改了 ruby on Rails 项目中 en.yml 文件中的属性名称。按钮工作正常。但字段属性并没有改变。
这是我的模型,
class Enr::AffordableWarmth < ActiveRecord::Base
self.table_name = "AffordableWarmth"
self.primary_key = 'Record_No'
validates_presence_of :No_Bedrooms, :No_Bedspaces, :Max_Annual_Energy_Cost
validates :No_Bedrooms, uniqueness: { scope: :No_Bedspaces,
message: "already exists!" }
validates_numericality_of :No_Bedrooms, :No_Bedspaces, :Max_Annual_Energy_Cost
end
en-GB.yml 文件
en-GB:
helpers:
submit:
enr_rds_dea:
create: 'Create new user'
update: 'Update'
enr_affordable_warmth:
create: 'Create'
update: 'Update'
activerecord:
models:
AffordableWarmth:
attributes:
AffordableWarmth:
No_Bedrooms: "Number of Bedrooms"
尽管如此,在 Rails 控制台中,它还是显示“没有卧室不能为空”。一些代码在 activerecord 中不起作用。在一些代码工作正常之前。
看着lib/active_model/errors.rb
,我认为关键应该是 'enr/affordable_warmth' 而不是 AffordableWarmth,但您可能需要以某种方式指定“Enr”命名空间。
如果它不起作用,请使用以下命令找到已安装的 gem 源bundle show activemodel
,然后进行一些调试lib/active_model/errors.rb
找出它需要什么 i18n 键。
Update:
调试方法如下:
我搜索 I18n,它负责翻译,所以我找到了方法generate_message
,它生成整个错误消息,并将翻译后的属性名称作为参数(请参阅:attribute
键),这引导我找到该方法human_attribute_name
,我发现在lib/active_model/translation.rb
.
我添加以下行:
puts "human_attribute_name(#{attribute.inspect}, #{options.inspect}): defaults: #{defaults.inspect}"
就在之前I18n.translate
,然后启动我的控制台。
对于一个模型Foo::User
I get:
human_attribute_name("account_type", {}): defaults: [:"activerecord.attributes.foo/user.account_type", :"attributes.account_type", "Account type"]
所以这应该适合你:
activerecord:
attributes:
"enr/affordable_warmth":
no_bedrooms: "Number of Bedrooms"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)