嵌套属性中的错误消息

Error messages in nested attribute

我有三个模型,分别是 AccountUserAccountPermission

我在创建用户时通过 AccountPermission 创建 Account

但是,如果出现与帐户 :name 相关的问题,系统会抛出类似下面的内容。

Account permissions account name has already been taken

所以,我只需要修正这个错误信息。

我已尝试将消息属性添加到我的验证中。它只是附加到实际消息。

我也尝试过语言环境。仍然只是追加

en:
  activerecord:
    errors:
      models:
        account:
          attributes:
            name:
              taken: 'bla bla'

据我所见ActiveModel。此消息的结构来自下面

locale/en.yml 在 ActiveModel

en:
  errors:
    format: "%{attribute} %{message}"

那么,有什么方法可以轻松编辑这条消息吗?如果我什至删除模型名称,就足够了。

错误消息由错误本身和属性名称连接而成,在提供的错误中为 account_permissions/account.name

您可以像这样为您的属性名称添加语言环境:

en:
  activerecord:
    attributes:
      account: # this is model name
        name: "Name"

en:
  activerecord:
    attributes:
      account_permissions/account:
        name: "Account name"