views:

903

answers:

3

In my rails app I use the validation helpers in my active record objects and they are great. When there is a problem I see the standard "3 errors prohibited this foobar from being saved" on my web page along with the individual problems.

Is there any way I can override this default message with my own?

+1  A: 

You can iterate over the model.errors hash yourself instead of using the errors helper.

Redbeard
I thought about going through through the errors hash in each case, but hoped there DRY way to do it in the model
RichH
+2  A: 

The "validates_" methods in your model can all generally be passed a :message => "My Validation Message" parameter.

I generally wrap errors in something like this:

<% if([email protected]?) %>
<div id="error_message">     
  <h2>
    <%= image_tag("error.png", :align => "top", :alt => "Error") -%>
    Oops, there was a problem editing your information.
  </h2>
  <%= short_error_messages_for(:model) %>
</div>
<% end %>

Then in my application_helper I iterate over the errors and generate a simple list:

  def short_error_messages_for(object_name)
    object = instance_variable_get("@#{object_name}")
    if object && !object.errors.empty?
       content_tag("ul", object.errors.full_messages.collect { |msg| content_tag("li", msg) } )     
    else
        ""
    end
  end

That code is pretty old and probably not how I would write Ruby these days, but you get the gist.

Toby Hede
+6  A: 

The error_messages_for helper that you are using to display the errors accepts a :header_message option that allows you to change that default header text. As in:

error_messages_for 'model', :header_message => "You have some errors that prevented saving this model"

The RubyOnRails API is your friend.

Yardboy
Perfect - thanks!
RichH
Worth mentioning: <%=f.error_messages :object_name=>'your information'%>
Yar