我可以为每个验证本地化一条错误消息,但是如何为特定模型创建错误。

普通语言环境如下所示:

en:
  mongoid:
    errors:
      messages:
        taken: "It is already taken"

但是我想更改user模型的消息:
en:
  mongoid:
    errors:
      messages:
        taken: "It is already taken"
        user:
          taken: "It is already taken. %{link_to 'Remember password', reset_password_path'}"

最佳答案

试试这个:

en:
  mongoid:
    errors:
      messages:
        taken: "It is already taken"
      models:
        user:
          attributes:
            login:
              taken: "It is already taken. %{link}"

引用:

ActiveRecord code comments

PS:
本地化字符串应使用插值变量进行动态替换。

例如:
login对象的user字段上添加错误:
user.errors.add(:login, :taken, :link => "foo")

关于ruby-on-rails - i18n : error message localization for particular model,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7125127/

10-10 11:46