# removing attribute prefix from validation error messages

**URL:** <https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389>\
**Category:** rubyonrails-talk\
**Created:** [September 26, 2009, 9:22am UTC](https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389 "2009-09-26T09:22:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgumbiker](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@bgumbiker](https://discuss.rubyonrails.org/u/bgumbiker)\
**Post date:** [September 26, 2009, 9:22am UTC](https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389/1 "2009-09-26T09:22:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ranjan\_kumar](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@ranjan\_kumar](https://discuss.rubyonrails.org/u/ranjan_kumar)\
**Post date:** [September 26, 2009, 10:50am UTC](https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389/2 "2009-09-26T10:50:34Z")

</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:** ![Richard\_Navarrete](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/richard_navarrete/32/11609_2.png) [@Richard\_Navarrete](https://discuss.rubyonrails.org/u/Richard_Navarrete)\
**Post date:** [September 26, 2009, 1:20pm UTC](https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389/3 "2009-09-26T13:20:15Z")

</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

&nbsp;&nbsp;HUMANIZED\_COLUMNS = {:msg =\> "Message"}

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

Richard

---

<div class="post-metadata">

**Author:** ![bgumbiker](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@bgumbiker](https://discuss.rubyonrails.org/u/bgumbiker)\
**Post date:** [September 26, 2009, 9:52pm UTC](https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389/4 "2009-09-26T21:52:39Z")

</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:** ![Richard\_Navarrete](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/richard_navarrete/32/11609_2.png) [@Richard\_Navarrete](https://discuss.rubyonrails.org/u/Richard_Navarrete)\
**Post date:** [September 27, 2009, 10:28pm UTC](https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389/5 "2009-09-27T22:28:39Z")

</div>

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

class MyModel \< ActiveRecord::Base

&nbsp;&nbsp;...

&nbsp;&nbsp;validates\_presence\_of :msg, :message =\> "can't be blank"

&nbsp;&nbsp;...

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:

\<name of column\> 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

&nbsp;&nbsp;...

&nbsp;&nbsp;validates\_presence\_of :msg, :message =\> "can't be blank"

&nbsp;&nbsp;HUMANIZED\_COLUMNS = {:msg =\> "Message"}

&nbsp;&nbsp;def self.human\_attribute\_name(attribute) &nbsp;&nbsp;&nbsp;&nbsp;HUMANIZED\_COLUMNS[attribute.to\_sym] || super &nbsp;&nbsp;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:** ![Richard\_Navarrete](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/richard_navarrete/32/11609_2.png) [@Richard\_Navarrete](https://discuss.rubyonrails.org/u/Richard_Navarrete)\
**Post date:** [September 27, 2009, 10:29pm UTC](https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389/6 "2009-09-27T22:29:58Z")

</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://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@bgumbiker](https://discuss.rubyonrails.org/u/bgumbiker)\
**Post date:** [September 27, 2009, 11:58pm UTC](https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389/7 "2009-09-27T23:58:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 29, 2009, 10:07am UTC](https://discuss.rubyonrails.org/t/removing-attribute-prefix-from-validation-error-messages/46389/8 "2009-09-29T10:07:17Z")

</div>

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

[http://github.com/gumayunov/custom-err-msg/](http://github.com/gumayunov/custom-err-msg/)
