![](/img/trans.png)
[英]Rails 4: remove attribute name from error message in custom validator
[英]Translating the length validator error message
在敏捷Web開發書中,建議進行如下單元測試:
assert_equal I18n.translate('activerecord.errors.messages.taken'),
product.errors[:title].join('; ')
我嘗試將其用於其他錯誤消息,例如長度驗證器too_short消息,但出現錯誤:
assert_equal I18n.translate('activerecord.errors.messages.too_short', :count=>10),
product.errors[:title].join('; ')
給出:
<"translation missing: en, activerecord, errors, messages, too_short"> expected but was
<"is too short (minimum is 10 characters)">.
谷歌快速搜索表明其他人正在使用activerecord.errors.messages.too_short,但顯然不起作用。 是否有其他訪問消息的方式,我在做其他錯誤嗎?
ActiveModel處理大多數錯誤,因此錯誤消息位於errors.messages
。 唯一例外的是taken
,因為其唯一性驗證的ActiveRecord具體。
因此,您需要:
assert_equal I18n.translate('errors.messages.too_short', :count=>10),
product.errors[:title].join('; ')
以下是消息: https : //github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.