2011-03-05 110 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