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

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

测验具有以下validation:

validates_presence_of :size, :style 

我正在使用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}}" 

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

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

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

任何人的想法?

应该还有:

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

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

我只是想出来,并认为我自己回答以防其他人有这个问题:我不得不修改我的翻译文件的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}}不能为空”例如。 因此,full_message即将出现,因为“{{attribute}} {{attribute}}不能为空”。 将其更改为“{{message}}”修复此问题。