2011-03-05 71 views
2

在我en.yml我有这样的:Rails 3是否缺少错误翻译?


en: 
    errors: 
    format: "%{message}" 
    messages: 
     blank:  "%{attribute} can't be blank" 
     invalid:  "%{attribute} is invalid" 
     too_short: "%{attribute} is too short" 
     too_long:  "%{attribute} is too long" 
     wrong_length: "%{attribute} is the wrong length" 
     taken:  "%{attribute}, {value}, is already taken" 

这是我的用户模型至今:


    validates_presence_of  :username 
    validates_uniqueness_of  :username 
    validates_length_of   :username, :within => 4..15 
    validates_format_of   :username, :with => /^\w+$/i 

    validates_presence_of  :password 
    validates_length_of   :password, :within => 6..20 

当我测试的随机数据,所有的错误信息工作的伟大,除了validates_uniqueness_of,它返回默认'已经被采纳'

非常感谢您提前。

回答

1

应该不会是

taken: "%{attribute}, %{value}, is already taken"

与价值百分号? 我不知道你可以访问value,但它是有道理的,否则可能是username。 我看到taken是正确的关键,但我仍然会尝试没有{value}看它是否工作。

最后或临时的解决办法,我认为你可以通过在你的模型验证的消息,这样的事情应该工作:

validates_uniqueness_of  :username, :mesage => "#{self.username} is already taken" 

但当然,你失去了很多的好处。

+0

非常感谢! – David 2011-03-07 22:01:00

+0

不客气,如果这解决了你的问题,你应该考虑选择接受的答案,这里左边的零。玩的开心! - 顺便说一下,什么解决方案帮助你? '%'符号? – ecoologic 2011-03-07 23:35:10

+0

是的,就是这样 - 抱歉,我今天没有意识到这个复选框,我会开始使用它。 – David 2011-03-27 00:18:51