2011-01-25 64 views
3

这一直困扰着我一段时间。所有模型都会出现此问题,但我将使用其中的一个测验作为示例。errors.full_messages:属性名称出现两次

测验具有以下验证:

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}}" 

问题是这样的:如果我做一个新的测验,这将无法通过验证,再看看在quiz.errors.full_messages中,每个错误消息都有属性,然后是完整的消息E:

>> quiz = Quiz.create 
=> <unsaved quiz object> 
>> 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"

任何想法吗?

+0

有你为什么需要添加`什么特别的原因{{属性}}`在每个验证消息的?通常情况下,那里的条目将只包含错误消息,例如“未包含在列表中”。该属性将根据您的语言环境文件中的`activerecord.errors.full_messages.format`自动添加,默认为``%{attribute}%{message}“` – sikachu 2011-01-25 11:31:41

+0

Hi Sikachu。这就是他们如何在供应商的轨道 - 我只是将该文件的内容复制到我的en.yml文件(注释掉),然后根据需要取消注释和修改。 – 2011-01-27 09:23:26

回答

8

有也应该是:

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

4

我想通了这一点,我想我会回答它自己的情况下,任何人都有这个问题:我不得不这样修改我的翻译的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}}”解决了这个问题。