# Removing attribute prefix from validation error messages

**URL:** <https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201>\
**Category:** Rails\
**Created:** [September 26, 2009, 11:24am UTC](https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201 "2009-09-26T11:24:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgumbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bgumbiker](https://www.ruby-forum.com/u/bgumbiker)\
**Post date:** [September 26, 2009, 11:24am UTC](https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201/1 "2009-09-26T11:24:31Z")

</div>

Hello,  
Any idea how to remove attribute name from validation error messages?  
thanks,  
bogumbiker

---

<div class="post-metadata">

**Author:** ![bgumbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bgumbiker](https://www.ruby-forum.com/u/bgumbiker)\
**Post date:** [September 26, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201/2 "2009-09-26T14:47:37Z")

</div>

Try this

class User \< ActiveRecord::Base  
validate do |user|  
user.errors.add\_to\_base(“This is my custom message”) if user.  
attribute\_name.blank?  
end  
end

---

<div class="post-metadata">

**Author:** ![bgumbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bgumbiker](https://www.ruby-forum.com/u/bgumbiker)\
**Post date:** [September 26, 2009, 3:22pm UTC](https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201/3 "2009-09-26T15:22:09Z")

</div>

Actually, bgumbiker, I just posted this answer yesterday so a bit more  
searching might have produced an answer.  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/cf59e09f98981f27/6c7fe5ff9dd016bc?lnk=gst&q=customize+active+record+attribute+display+name+in+error+message#6c7fe5ff9dd016bc](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/cf59e09f98981f27/6c7fe5ff9dd016bc?lnk=gst&q=customize+active+record+attribute+display+name+in+error+message#6c7fe5ff9dd016bc)

If you used Ranjan’s answer, you’ll have to enter a specific custom  
message for each validation you do. If you use the solution I  
outlined in that URL or below, you change the “humanization” version  
of that column name so you can do validations as normal and when the  
human name is used to display an error message, it’ll use the one you  
mapped.

class Post \< ActiveRecord::Base

HUMANIZED\_COLUMNS = {:msg =\> “Message”}

def self.human\_attribute\_name(attribute)  
HUMANIZED\_COLUMNS[attribute.to\_sym] || super  
end  
end

Richard

---

<div class="post-metadata">

**Author:** ![bgumbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bgumbiker](https://www.ruby-forum.com/u/bgumbiker)\
**Post date:** [September 26, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201/4 "2009-09-26T23:54:16Z")

</div>

THanks Richard,  
Could you post any further usage example as I am quite new in Ruby and  
Rails and do not understand what happening below in your code. Is :msg  
an attribute in the model?  
thanks,  
bogumbiker

---

<div class="post-metadata">

**Author:** ![bgumbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bgumbiker](https://www.ruby-forum.com/u/bgumbiker)\
**Post date:** [September 28, 2009, 12:30am UTC](https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201/5 "2009-09-28T00:30:26Z")

</div>

Your validation error message is coming from your model. In your  
model, you have something like this:

class MyModel \< ActiveRecord::Base

…

validates\_presence\_of :msg, :message =\> “can’t be blank”

…

end

When you submit a form you are validating that the message someone  
enters in the column field you created in your database, which is  
called “msg” actually has something in it (validates\_presence\_of).  
Here, we’re saying when someone submits a form and the “msg” field is  
empty, give them this error:

can’t be blank

The name of the column here is “msg.” But you don’t want to display  
that in your error back to the client. You want it to say something  
nice like “Message can’t be blank” not “msg can’t be blank.”

So, in my method, we take the same model we have above and add the  
humanize mapping.

class MyModel \< ActiveRecord::Base

…

validates\_presence\_of :msg, :message =\> “can’t be blank”

HUMANIZED\_COLUMNS = {:msg =\> “Message”}

def self.human\_attribute\_name(attribute)  
HUMANIZED\_COLUMNS[attribute.to\_sym] || super  
end

end

With that method, you’re just mapping a hash of symbols (in this case  
you just have one - :msg) and modifying the human\_attribute\_name  
method that rails uses when displaying the validation error. You see,  
when the validation fails (the value for the field was not present) it  
spits out the error containing the (humanized version) of the column  
name with the :message you gave it. The validation error is always  
humanizing the column name, but it can’t make much of “msg” so it  
leaves it alone. I always suggest giving column names (in your  
database / model) when you set it up to be good column names it can  
humanize. But, if you have to have one like “msg,” then you can work  
around it when it tries to humanize it.

To break down the method just a little, “human\_attribute\_name” is a  
method in ActionRecord, but it’s present here since your class  
“MyModel” inherits from ActionRecord::Base. You are essentially  
modifying it by "def"ining it here. You’re just saying, when you  
“humanize” a column, for whatever reason, see if the column name is in  
my hash “HUMANIZED\_COLUMNS” and use that value instead of the column  
name you’re going to try to humanize.

Hope that helps,  
Richard

---

<div class="post-metadata">

**Author:** ![bgumbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bgumbiker](https://www.ruby-forum.com/u/bgumbiker)\
**Post date:** [September 28, 2009, 12:31am UTC](https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201/6 "2009-09-28T00:31:36Z")

</div>

“Yes” is the short answer, but I was hoping that explaining more about  
it you’d get a little more out of my answer.

Cheers,  
Richard

---

<div class="post-metadata">

**Author:** ![bgumbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bgumbiker](https://www.ruby-forum.com/u/bgumbiker)\
**Post date:** [September 28, 2009, 2:08am UTC](https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201/7 "2009-09-28T02:08:12Z")

</div>

Thanks Richard!,  
It is now clear I like it!  
bogum

---

<div class="post-metadata">

**Author:** ![bgumbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@bgumbiker](https://www.ruby-forum.com/u/bgumbiker)\
**Post date:** [September 29, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/removing-attribute-prefix-from-validation-error-messages/176201/8 "2009-09-29T12:07:17Z")

</div>

You can use custom error message plugin to display custom message…

> **[GitHub - gumayunov/custom-err-msg: Fork of...](https://github.com/gumayunov/custom-err-msg/)**
>
> Fork of svn://rubyforge.org/var/svn/custom-err-msg - GitHub - gumayunov/custom-err-msg: Fork of svn://rubyforge.org/var/svn/custom-err-msg
