method

error_messages_for

error_messages_for(object_name, options = {})
public

Returns a string with a div containing all the error messages for the object located as an instance variable by the name of object_name. This div can be tailored by the following options:

  • header_tag - Used for the header of the error div (default: h2)
  • id - The id of the error div (default: errorExplanation)
  • class - The class of the error div (default: errorExplanation)

NOTE: This is a pre-packaged presentation of the errors with embedded strings and a certain HTML structure. If what you need is significantly different from the default presentation, it makes plenty of sense to access the object.errors instance yourself and set it up. View the source of this method to see how easy it is.

8Notes

Friendlier error message example

autonomous · Jul 28, 200815 thanks

The default error messages can be a bit stale and off putting. Try somethings like this:

error_messages_for( :user, :header_message => "Oops - We couldn't save your user!", :message => "The following fields were a bit of a problem:", :header_tag => :h1 )

You can also use error_messages_for as follows <% form_for User.new do |f| %> <%= f.error_messages :header_message => "..." %> <% end %>

Overriding the default div class="fieldWithErrors"

hosiawak · Aug 12, 200812 thanks

By default fields that are invalid are wrapped in:

<div class="fieldWithErrors">
<input type="text" name="blah">
</div>

To override and wrap in spans instead of divs place the following in your environment.rb:

ActionView::Base.field_error_proc = Proc.new { |html_tag, instance| "<span class=\\"fieldWithErrors\\">#{html_tag}</span>" }

or to not use wrapping at all:

ActionView::Base.field_error_proc = Proc.new { |html_tag, instance| "#{html_tag}" }

Customizing attribute names in error messages

tarvaina · Oct 23, 20088 thanks

By default, the error messages translate the names of the attributes through String#humanize. The way to to change that is to override the ActiveRecord::Base.human_attribute_name method.

For example, if you want to name a column in your database as :www_url and you want to say "Website" instead of "Www url" in the error message, you can put this into your model:

class Person < ActiveRecord::Base
def self.human_attribute_name(attribute_key_name)
  if attribute_key_name.to_sym == :www_url
    "Website"
  else
    super
  end
end
end

Currently this seems to be the cleanest and easiest way. Unfortunately, human_attribute_name is deprecated and may stop working in a future release of Rails.

Detailed messages for a nested model

grosser · Aug 4, 20083 thanks

Detailed messages for a nested model

<%@address = @order.address%>
<%=error_messages_for :address%>

Override fieldWithErrors markup in Rails > v2

greenideas · Mar 31, 20093 thanks

The code posted by @hosiawak will still work in recent versions of Rails, but maybe a more current, idiomatic way to do it is to stick this inside the Rails::Initializer block in environment.rb (obviously you'll also need to restart your server to pick up the config change):

config.action_view.field_error_proc = Proc.new {|html_tag, instance| 
%(<span class="fieldWithErrors">#{html_tag}</span>)}

re: Customizing attribute names in error messages

roywblack · Oct 30, 20082 thanks

You can use Module.alias_attribute to achieve the same result as overriding human_attribute_name. After aliasing use the new name in the validates_xxxx_of methods or ActiveRecord::Errors.add.

Use Hpricot to customise error fields

rob-twf · Aug 14, 20081 thank

I like to {use Hpricot}[http://www.thewebfellas.com/blog/2008/4/21/error-fields-with-a-hpricot-twist] to add error information to my form fields. Here's an example:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<(input|label|textarea|select)/
  error_class = 'error'
  nodes = Hpricot(html_tag)
  nodes.each_child { |node| node[:class] = node.classes.push(error_class).join(' ') unless !node.elem? || node[:type] == 'hidden' || node.classes.include?(error_class) }
  nodes.to_html
else
  html_tag
end
end

This will only apply the CSS class 'error' to elements that aren't hidden inputs and don't already have the error class.

Sample output:

<div>
<label class="error" for="user_email">Email</label>
<input name="user[email]" size="30" class="error" type="text" id="user_email" value="" />
</div>

Only error message

RobinWu · Oct 16, 2008

=== <%= error_messages_for :order, :header_message => nil, :message => nil %>

Browser view code

<div id="errorExplanation" class="errorExplanation">
<ul>
  <li>Weight 只有 1000.0</li>
  <li>Volume 只有 10.0</li>
</ul>
</div>