errors.full_messages:属性名称出现两次

Max*_*ams 4 validation activerecord ruby-on-rails

这一直困扰着我.我的所有模型都会出现此问题,但我会使用其中一个,测验,作为示例.

测验具有以下验证:

validates_presence_of :size, :style
Run Code Online (Sandbox Code Playgroud)

我正在使用I18n,我在我的翻译文件中有以下设置:(这些只是标准的错误消息,但我已将它们包含在我的en.yml中,以便很容易看到结构,如果我想要为任何特定型号覆盖它们)

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}}"   
Run Code Online (Sandbox Code Playgroud)

问题是这样的:如果我做一个新的测验,验证失败,那么看看quiz.errors.full_messages,每个错误消息都有属性然后是完整的消息:

>> quiz = Quiz.create
=> <unsaved quiz object>
>> quiz.errors.full_messages
=> ["Size Size can't be blank", "Style Style can't be blank"]
Run Code Online (Sandbox Code Playgroud)

我不明白为什么消息是,例如,"Size Size can't be blank"而不是"Size can't be blank"

任何人的想法?

ger*_*tas 8

还应该:

en:
  errors:
    # The default format to use in full error messages.
    format: "%{attribute} %{message}"
Run Code Online (Sandbox Code Playgroud)

而您的其他翻译不应再包括在内%{attribute}.为了确保en.yml从Rails版本中正确使用它,它位于:lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/locale/en.yml


Max*_*ams 5

我只是想出来并认为我自己回答以防其他人有这个问题:我不得不修改我的翻译文件的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"
Run Code Online (Sandbox Code Playgroud)

问题是,activerecord.errors.full_messages.format密钥设置为(vendor/rails/activerecord/lib/active_record/locale/en.yml{)到"{{attribute}} {{message}}",并且消息依次设置为"{{attribute}}不能为空".因此,full_message即将出现,因为"{{attribute}} {{attribute}}不能为空".将其更改为"{{message}}"修复此问题.