2010-02-13 55 views
1

希望有人能指出我朝着正确的方向。我有一个en.yml文件属性名称在错误消息中重复两次

en: 
    hello: "Hello world" 
    activerecord: 
    models: 
     recipe: "Recipe" 
    attributes: 
     recipe: 
     title: "Recipe title" 
    errors: 
     models: 
     recipe: 
      attributes: 
      title: 
       blank: "{{attribute}} cannot be left blank" 

在我的模型我验证标题字段的存在

validates_presence_of :title 

但是,我在页面上看到错误消息是这样的

Recipe title Recipe title cannot be left blank. 

我不明白为什么属性名称重复两次。

任何想法?

回答

2

由于rails会将错误消息与属性名称作为前缀。消息中不需要{{attribute}}。这将按预期工作:

en: 
    hello: "Hello world" 
    activerecord: 
    models: 
     recipe: "Recipe" 
    attributes: 
     recipe: 
     title: "Recipe title" 
    errors: 
     models: 
     recipe: 
      attributes: 
      title: 
       blank: "cannot be left blank" 
+0

所以我可以在错误消息前加上“Recipe {{attribute}}某条消息”吗? – iJK 2010-02-13 20:46:05

+0

据我所知,你不能这样做(至少不是为了“空白”消息),因为名称仍然会在消息的开头添加。 – 2010-02-13 20:48:36

+0

相关知识,谢谢Tomas – iJK 2010-02-13 20:50:52