3

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.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
Sri
  • 2,233
  • 4
  • 31
  • 55

1 Answers1

5

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"
Leventix
  • 3,789
  • 1
  • 32
  • 41
  • Hi, Thanks. But there is not active_model folder inside the lib folder. :( – Sri Jan 07 '13 at 16:12
  • 1
    Did you look in the lib folder in of the `activemodel` gem? On my machine it's in a directory like this: `/home/lev/.rvm/gems/ruby-1.9.2-p180@foo/gems/activemodel-3.2.8/lib/active_model/` – Leventix Jan 07 '13 at 16:17
  • thank you. Now, i am in error.rb file inside the active_model folder. But i am confused bcz its a very long file. am a newbie in ruby on rails. What should i edit in this? – Sri Jan 07 '13 at 16:21
  • 1
    added debugging instructions to the post above – Leventix Jan 07 '13 at 16:47
  • It works for me with this pattern. You can try `I18n.t "activerecord.attributes.enr/affordable_warmth.no_bedrooms"` in the console, to see if the translation file is loaded correctly. Try making the indentation to 2 spaces on every level, although it works for me with 4 spaces too. – Leventix Jan 07 '13 at 17:41
  • Hi, I did the same of your above comment and it shows the translation file is loaded correctly. But got the same problem in the front end.. any idea? – Sri Jan 08 '13 at 12:25