# Customizing error messages on ActiveRecord

**URL:** <https://www.ruby-forum.com/t/customizing-error-messages-on-activerecord/127089>\
**Category:** Rails\
**Created:** [January 24, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/customizing-error-messages-on-activerecord/127089 "2008-01-24T21:32:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremysdr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jeremysdr](https://www.ruby-forum.com/u/jeremysdr)\
**Post date:** [January 24, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/customizing-error-messages-on-activerecord/127089/1 "2008-01-24T21:32:21Z")

</div>

Hi Everyone,

Is there a gem plugins that can customize the built-in error messages  
that we are seeing when there’s an error an ActiveRecord?

I am referring on this message

“7 errors prohibited this RECORD from being saved”

---

<div class="post-metadata">

**Author:** ![jeremysdr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jeremysdr](https://www.ruby-forum.com/u/jeremysdr)\
**Post date:** [January 24, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/customizing-error-messages-on-activerecord/127089/2 "2008-01-24T21:40:49Z")

</div>

# Haven’t seen a plugin like that. Though you could rather easily create your own partial to do this by accessing the errors directly through

# @record.errors

Personally, I have always favored designs that make use of the  
“error\_message\_on” helper

---

<div class="post-metadata">

**Author:** ![jeremysdr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jeremysdr](https://www.ruby-forum.com/u/jeremysdr)\
**Post date:** [January 25, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/customizing-error-messages-on-activerecord/127089/3 "2008-01-25T15:43:58Z")

</div>

Ok… After a few ‘google’ session, I finally found this solution and I  
hope it will helps you out there.

Insert this block of code on your application\_helper.rb (found on  
app/helper)

* * *

def error\_messages\_for(object\_name, options = {})  
options = options.symbolize\_keys  
object = instance\_eval “@#{object\_name}”  
unless object.errors.empty?  
content\_tag(“div”,  
content\_tag(options[:header\_tag] || “h2”, “WARNING:”) +  
content\_tag(“ul”, object.errors.full\_messages.collect { |msg|  
content\_tag(“li”, msg) }),  
“id” =\> options[:id] || “errorExplanation”, “class” =\>  
options[:class] || “errorExplanation”  
)  
end  
end #def error\_messages\_for

* * *

Thats it… !!! a customized error message header for your ActiveRecord  
error message box. 😉
