Changing attributes name in en.yml file is not working

做~自己de王妃 提交于 2019-12-12 12:27:43

问题


I changed the attributes name in en.yml file in ruby on rails project. The buttons are working fine. But the field attributes is not changing.

Here is my model,

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 file

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"

Still, in the rails console and in the form it displays 'No Bedrooms could not be blank'. Bit of code is not working from the activerecord. Before bit of code is working fine.


回答1:


Looking at lib/active_model/errors.rb, I think the key should be 'enr/affordable_warmth' instead of AffordableWarmth, but you probably need to specify the "Enr" namespace in some way.

If it doesn't work, locate the installed gem source with bundle show activemodel, then do some debugging in lib/active_model/errors.rb to find out exactly what i18n keys it expects.

Update:

Here's how to debug this:

I search for I18n, which does the translaton, so I find the method generate_message, which generates the whole error message, and it takes the translated attribute name as a paramater (see :attribute key), which leads me to the method human_attribute_name, and I find that in lib/active_model/translation.rb.

I add the line:

puts "human_attribute_name(#{attribute.inspect}, #{options.inspect}): defaults: #{defaults.inspect}"

just before the I18n.translate, and start my console.

For a model Foo::User I get:

human_attribute_name("account_type", {}): defaults: [:"activerecord.attributes.foo/user.account_type", :"attributes.account_type", "Account type"]

So this should work for you:

  activerecord:
      attributes:
        "enr/affordable_warmth":
          no_bedrooms: "Number of Bedrooms"


来源:https://stackoverflow.com/questions/14199285/changing-attributes-name-in-en-yml-file-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!