开发者

Translating the length validator error message

开发者 https://www.devze.com 2023-01-29 18:13 出处:网络
In the Agile Web Development book, it is suggested to make a unit test as follows: assert_equal I18n.translate(\'activerecord.errors.messages.taken\'),

In the Agile Web Development book, it is suggested to make a unit test as follows:

assert_equal I18n.translate('activerecord.errors.messages.taken'),
             product.errors[:title].join('; ')

I tried using this for other error messages, such as the length validator too_short message, but get errors:

assert_equal I18n.translate('activerecord.errors.messages.too_short', :count=>10),
             product.errors[:title].join('; ')

gives:

<"translation missing: en, activerecord, errors, messages, too_short"> expected but was
<"is too short (minimum is 10 characters)">.

A quick google search suggests others are using activerecord.errors.messages.too_short, but apparently it doesn't work. Is there a different way to access the message开发者_JAVA百科, am I doing something else wrong?


ActiveModel handles most of the errors, so the error messages are in errors.messages. The only exception is taken, because the uniqueness validation is ActiveRecord specific.

So you'll need:

assert_equal I18n.translate('errors.messages.too_short', :count=>10),
             product.errors[:title].join('; ')

Here are the messages: https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml

0

精彩评论

暂无评论...
验证码 换一张
取 消