4

This has been bugging me for a while. This problem occurs with all of my models, but i'll use one of them, Quiz, as an example.

Quiz has the following validations:

validates_presence_of :size, :style

I'm using I18n, and i have the following set in my translations file: (these are just the standard error messages, but i've included them in my en.yml so that it's easy to see the structure, if i want to override them for any particular model)

activerecord:
  errors:
    messages:
      inclusion: "{{attribute}} is not included in the list"
      invalid: "{{attribute}} is invalid"
      empty: "{{attribute}} can't be empty"
      blank: "{{attribute}} can't be blank"
      record_invalid: "Validation failed: {{errors}}"   

The problem is this: if i make a new quiz, which will fail validation, then look at quiz.errors.full_messages, each error message has the attribute then the full message:

>> quiz = Quiz.create
=> <unsaved quiz object>
>> quiz.errors.full_messages
=> ["Size Size can't be blank", "Style Style can't be blank"]

I don't understand why the message is, for example, "Size Size can't be blank" and not "Size can't be blank"

Any ideas anyone?

Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • Is there any particular reason why you need to add `{{attribute}}` in each of the validation message? Normally, the entry there would contain only the error message, such as "is not included in the list". The attribute will automatically added based on the `activerecord.errors.full_messages.format` on your locale file, which defaults as `"%{attribute} %{message}"` – sikachu Jan 25 '11 at 11:31
  • Hi Sikachu. That's how they were in vendor rails - i just copied out the contents of that file into my en.yml file (commented out) and then uncomment and amend as necessary. – Max Williams Jan 27 '11 at 09:23

2 Answers2

8

There should be also:

en:
  errors:
    # The default format to use in full error messages.
    format: "%{attribute} %{message}"

And your other translations shouldn't include %{attribute} anymore. To make sure you get all correctly use en.yml from your Rails version, it is located at: lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/locale/en.yml

gertas
  • 16,869
  • 1
  • 76
  • 58
  • Thanks Gertas, that's what i figured out too, though i based mine on vendor rails rather than activemodel 3 (i'm using rails 2.3.4) – Max Williams Jan 27 '11 at 09:22
5

I just figured this out and thought i'd answer it myself in case anyone else had this problem: i had to amend the activerecord part of my translations file thus:

activerecord:
  errors:
    full_messages:
      format: "{{message}}"    
    #define standard error messages, which we can overide on per model/per attribute basis further down
    messages:
      inclusion: "{{attribute}} is not included in the list"
      exclusion: "{{attribute}} is reserved"

The problem was that the activerecord.errors.full_messages.format key was set (in vendor/rails/activerecord/lib/active_record/locale/en.yml) to "{{attribute}} {{message}}", and the messages in turn were set to "{{attribute}} can't be blank" for example. So the full_message was coming out as "{{attribute}} {{attribute}} can't be blank". Changing it to be just "{{message}}" fixed this.

Max Williams
  • 32,435
  • 31
  • 130
  • 197