errors.full_messages: attribute name appears twice
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 b开发者_开发问答e 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?
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
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.
精彩评论