VegaStudios
VegaStudios

Reputation: 324

Remove attribute name from locales error message

I've got a set of locales, as listed below. When an error message is triggered, it will pull in the name of the attribute and prepends it before the error message.

The error result for the points value being blank on submit is, "value points value can't be blank".

How do I remove the {%attribute} name in the error message?

en:
 activerecord:
    attributes:
      referral:
        email: The email address you entered
    errors:
      models:
        answer:
          attributes:
            value:
              blank: points value can't be blank

I've also tried to add the message in the model, but to no avail (it still prepends the attribute name).

validates_presence_of :value, :message => "points value can't be blank"

Thanks in advance!

Upvotes: 1

Views: 747

Answers (2)

Sebastián Palma
Sebastián Palma

Reputation: 33420

You could try accessing each message value from the errors in the object you're trying to create, and within each message, to access it's first value (as it's an array), something like:

<% answer.errors.messages.values.each do |message| %>
  <li><%= message.first %></li>
<% end %>

Upvotes: 1

csexton
csexton

Reputation: 24783

I would do this by localizing the attribute name, rather than prevent it from being added to the message:

en:
  activerecord:
    attributes:
      answer:
        value: "points"

Upvotes: 1

Related Questions