Alex Wayne
Alex Wayne

Reputation: 186984

Rails 3 i18n, can't seem to customize model attribute name

In Rails 3, I'm trying to customize the name of some model attributes when they appear in form errors. My User model has a field named initial_zip, and I would like to present errors on this field in the name Zip code instead.

It looks like I'm supposed to do this via locales. So I added the following to my en.yml

config/locales/en.yml

en:
  active_record:
    attributes:
      user:
        initial_zip: Zip code

However, when I register a new user without an initial_zip I get the validation error message:

"Initial zip can't be blank"

I expected to get this instead:

"Zip code can't be blank"

I did find this in snippet in application.rb, but the comment seems to say that the default is just what I want (:en default locale and loading form config/locales). So I left it commented.

config/application.rb

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de

Any idea what I did wrong here?

Upvotes: 0

Views: 160

Answers (1)

Jeremy Green
Jeremy Green

Reputation: 8574

I think the problem is that you need quotes around your Zip code string.

en:
  active_record:
    attributes:
      user:
        initial_zip: "Zip code"

Upvotes: 1

Related Questions