Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 578
Max Williams Avatar asked Dec 12 '22 16:12

Max Williams


1 Answers

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

like image 199
gertas Avatar answered Jan 06 '23 02:01

gertas