Reputation: 45134
I have the following validation error messages coming up when I try to submit a blank form:
Start time time Looks like you forgot the appointment start time.
Start time time Sorry, we can't understand "" as a time.
Start time ymd Please choose a date for the appointment.
Start time ymd Sorry, we can't understand "" as a date.
Stylist services Please choose at least one service.
These messages are for the following attributes:
start_time_time
start_time_time
start_time_ymd
start_time_ymd
stylist_services
I included the attribute names so you could plainly see which part of the error message is the attribute name.
How do I remove the attribute names from the error messages?
Upvotes: 9
Views: 12106
Reputation: 6757
In rails 3.2.6, you can supress the inclusion of the attribute name by setting errors.format in a locale file (e.g., config/locales/en.yml):
en:
errors:
format: "%{message}"
Otherwise, the default format is "%{attribute} %{message}".
Upvotes: 27
Reputation: 391
I did almost the same thing as Brandon.
First, I wrote a helper function for the object that the errors will render for.
#Remove unnecessary attribute names in error messages
def exclude_att(attribute, error_msg)
if attribute.to_s == "Put attribute name you don't want to see here"
error_msg
else
attribute.to_s.humanize + " " + error_msg
end
end
Then, in the view that has the form being validated, I did: (Note: this is HAML code not HTML, but the tags are still the same so you can clearly see what I'm doing)
%h3= "#{pluralize(@user.errors.count, 'error')} prohibited this user from being saved:"
%ul
- @user.errors.each do |att, error|
%li= exclude_att(att, error)
That did it for me, no gems or third-party plugins.
-Demitry
Upvotes: 0
Reputation: 159115
It's common to loop over object.full_messages
to output each full message:
<% if object.errors.any? %>
<h2>Errors</h2>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<h2>Errors</h2>
<ul>
<li>Start time time Looks like you forgot the appointment start time.</li>
<li>Start time time Sorry, we can't understand "" as a time.</li>
<li>Start time ymd Please choose a date for the appointment.</li>
<li>Start time ymd Sorry, we can't understand "" as a date.</li>
<li>Stylist services Please choose at least one service.</li>
</ul>
But a "full" message consists of the localized field name followed by the message (as you've seen; this is because the messages are usually things like "can't be blank"). If you just want the actual error message minus the field name, use the built-in each
iterator instead:
<% if object.errors.any? %>
<h2>Errors</h2>
<ul>
<% object.errors.each do |field, msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<h2>Errors</h2>
<ul>
<li>Looks like you forgot the appointment start time.</li>
<li>Sorry, we can't understand "" as a time.</li>
<li>Please choose a date for the appointment.</li>
<li>Sorry, we can't understand "" as a date.</li>
<li>Please choose at least one service.</li>
</ul>
Upvotes: 23
Reputation: 65497
You could use the i18n route to change the display name of the attribute.
en:
activerecord:
attributes:
somemodel:
start_time_time: My Start Time Text #renamed text
stylist_services: "" #hidden txet
Upvotes: 18