Reputation: 7398
I cannot get rid of the attribute in the error message. (I wonder why this isn't the default behaviour)
Controller
if @item.save
...
else
format.html { redirect_to [@profile, :items], alert: @item.errors.full_messages}
end
it.yml
it:
errors:
format: "%{message}"
View.html
<% if alert %>
<% Array.wrap(alert).each do |msg| %>
<%=msg%>
<% end %>
<% end %>
But i get this output
ATTRIBUTE NAME my custom message
Upvotes: 3
Views: 2644
Reputation: 4756
You need to enable the feature to allow overriding the error message format, as it is turned off by default.
in config/application.rb
config.active_model.i18n_customize_full_message = true
Then you can customise format either globally, at the model level, or the attribute level. e.g.
it:
activerecord:
errors:
models:
model_name:
attributes:
attribute_name:
not_a_number: "My custom message"
greater_than: "My custom message"
format: "%{message}"
(replace model_name
and attribute_name
and the validation keys as appropriate).
The i18n-debug
gem is also handy for debugging i18n issues.
Upvotes: 0
Reputation: 33460
Use messages
instead of full_messages
:
@item.errors.messages.values.flatten
As messages
returns a hash, where the key shows what's the validation error, you can simply get and flatten all values.
{ :email=>["Another record exists with this email."], ... } # for example
Upvotes: 6