Better messages for ActiveRecord validation errors 21
So you’ve been working with Rails for some time and you think you know what you’re doing, right? And you know to remove all your scaffolding before you ship, right?
Suppose you’re writing an app to let two people play a game over them thar tubes.
Would you still do this?
<%= error_messages_for :game %>
Do you realize that your users will see something like this?

error_messages_for is scaffolding in disguise. It will help you get going but it’s meant to be thrown away. Only computer geeks like to know about “fields” and things being “saved.”
Your users just want to start a game. Treat them like humans and specify your own header and subtitle messages. (Rails documentation does exist despite everyone’s complaints that there’s “no documentation”).
So how about something like this instead:
<%= error_messages_for :game, :header_message => "Please Try Again!", :message => "We had some problems starting the game:" %>
which will produce something like:




Damn, and all this time I was hacking ActiveRecord to achieve the same effect! Thanks for the useful info.
Same as sabon, i overloaded the method... really good to know thanks for the tip :)
awesome
Uh... It doesn't work for me. Rails 2.0.2:
<%= errormessagesfor :contact, :header_message => "Uh Oh!", :message => "There are some problems with this information:")%>
Any ideas?
@Dan - don't you mean: errormessagesfor ? and if 'it doesn't work', how does it not work.
When I submitted this patch and it got pushed to edge, I was surprised at the lack of love it received. I keep thinking about extending it a bit more too and allowing for more options.
@Zach: Thanks for the patch. Your suprise at the lack of interest is, I think, because people either don't realize the default wording isn't the best, or because they tend to do something else entirely for showing the @.errors@ collection so they can customize the display.
don't work to me too.
i'm using last version in rails git repository.
@Zack: we're getting markdowned or textiled, our underscores are getting turned into italics. I'm calling the correct method.
Anyway by not work I mean that the default error messages are still showing up. I found that my problem is that gettextlocalize is overriding the behavior. I haven't found the code which defines this override but I'm sure that is the problem.
The problem was the gettext gem. The gem exposes the methods below which I call before the error messages for.
<%= ActionView::Helpers::ActiveRecordHelper::L10n.seterrormessagetitle(("Uh Oh!"), _("Uh Oh!"))
ActionView::Helpers::ActiveRecordHelper::L10n.seterrormessageexplanation(("Please correct the following problems:"), _("Please correct this problem:"))
errormessagesfor :contact %>
Usefull post, thank you!
Thank you, works great, i am on Rails 2.0.2.
PJ.
Thank you for posting this, extremely useful! Also worth noting that to customize specific field errors just do validates_presence_of :field_name, :message => “My error”
@Dan: To make it work with Gettext, you can do the following:
<%= error_messages_for :game, { :message_title => Nn_("Singular Custom Error message %{record}: %{num}", "Plural Custom Error message %{record}: %{num}"), :message_explanation => Nn_("Singular Custom Error explanation %{num}", "Plural Custom Error explanation %{num}") } %>It’s explained in ruby-gettext’s doc:
http://www.yotabanana.com/hiki/ruby-gettext-howto-rails.html?ruby-gettext-howto-rails#Translate+the+title%2Fexplanation+on+the+error+message+dialog
Because of the relation between our knowledge and the paralogisms, the reader should be careful to observe that the discipline of natural reason depends on the objects in space and time; by means of the manifold, the architectonic of practical reason is a representation of our experience.
As is shown in the writings of Hume, what we have alone been able to show is that the things in themselves (and Aristotle tells us that this is the case) would thereby be made to contradict the noumena.
I also could not seem to override params for error_messages_for. Looked at the documentation, tried to override everything, no luck. I wonder if maybe some plugins were causing problems, but I dont think so. Anyways, I found a super easy way around this. I simply added display:none in the css for the h2 tag and the p tag for errorExplanation. Took me 2 seconds instead of trying to figure out wtf is the problem with error_message_for.
Also, dude, your page is screwy when you try to leave a comment. Jumps to another page, cant see any prior comments. After save, i get buggy page back. Your welcome
First of all thanx for the post…secondly; how do you remove the messages with the bullets?
Thank you for this good article!
Combine this with http://henrik.nyh.se/2007/12/change-displayed-column-name-in-rails-validation-messages and you have all you need to satisfy your copy editors.
That’s very useful. Works fine on Rails 2.0.2, but don’t forget to restart the server. =d