utkuDAT
utkuDAT

Reputation: 355

Error messages in nested attribute

I've three models called Account,User and AccountPermission.

I'm creating the Account via AccountPermission while creating user.

However, If a problem occurs related with Account :name, the system throws something like that below.

Account permissions account name has already been taken

So, I just need to fix this error message.

I've tried to add a message attribute to my validation. It is just appending to actual message.

I've also tried locale thing. Still just appending

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

As far as I see in ActiveModel. This message's structure comes from below

locale/en.yml in ActiveModel

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

So, Is there any way edit this message painless ? If I even delete the model name, It's enough.

Upvotes: 2

Views: 2259

Answers (1)

Vasfed
Vasfed

Reputation: 18504

Error message is concatenated from the error itself and the attribute name, which is account_permissions/account.name in the provided error.

You can add locale for your attribute names like so:

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

or

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

Upvotes: 3

Related Questions