Errors.full_messages: attribute name is displayed twice

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

Quiz has the following checks:

validates_presence_of :size, :style

I use I18n and I have the following set in my translation file: (these are only standard error messages, but I included them in my en.yml so that it is easy to see the structure if I want to override them for any specific 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 that fails the test, then look at quiz.errors.full_messages, each error message has an attribute, and 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 do not understand why the message, for example "Size Size can't be blank", and not"Size can't be blank"

Any ideas anybody?

+3
2

:

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

%{attribute}. , en.yml Rails, : lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/locale/en.yml

+8

, , - : activerecord :

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"

, activerecord.errors.full_messages.format ( vendor/rails/activerecord/lib/active_record/locale/en.yml) "{{attribute}} {{message}}", "{{attribute}}, t be blank", . , full_message "{{attribute}} {{attribute}} ". , "{{message}}" .

+4

Source: https://habr.com/ru/post/1787646/


All Articles