Forum: Ruby on Rails custom errors for validates_presence, etc..

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E884d559ed311e48ec9831b895719b2a?d=identicon&s=25 Yanni Mac (yannimac)
on 2007-06-10 04:47
I am trying to figure out how to customize the error messages when I use
validates_presence_of.  I am validating a user name and I use <%=
error_messages_for 'user' %> in my view to display the messages.  I know
you can customize in the model with :message =>'custom message', but I
am trying to get rid of the first part of the error message which is,

1 error prohibited this user from being saved
There were problems with the following fields:

Is there any way to do this or is it hard coded into the rails code?
069b7107c7bc28cf78af2f0e31e94c62?d=identicon&s=25 [s] (Guest)
on 2007-06-21 20:56
(Received via mailing list)
I'm struggling with the same right now.
Does anybody know how to do this?

813f535246722b7bf02aacc9ce818de8?d=identicon&s=25 Bob Showalter (Guest)
on 2007-06-21 22:08
(Received via mailing list)
On 6/9/07, Yanni Mac <> wrote:
> Is there any way to do this or is it hard coded into the rails code?
The basic idea is to look at the source for error_messages_for and
then drop your own replacement into app/helpers/application_helper.rb.

Here's a *real* simple example:

  def error_messages_for(object_name)
    object = instance_variable_get("@#{object_name}")
    return '' if object.errors.empty?
    content_tag('ul', object.errors.full_messages.collect {|msg|
content_tag('li', msg)},
      { :class => 'error' })
This topic is locked and can not be replied to.