# How to change the error message easy way

**URL:** <https://www.ruby-forum.com/t/how-to-change-the-error-message-easy-way/64759>\
**Category:** Rails\
**Created:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/how-to-change-the-error-message-easy-way/64759 "2006-08-15T14:18:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/how-to-change-the-error-message-easy-way/64759/1 "2006-08-15T14:18:44Z")

</div>

validates\_presence\_of :fname

results in the error message

“Fname can’t be blank”.

What I want is “First Name can’t be blank”.

I could do this

def validate  
errors.add\_to\_base(“First Name can’t be blank”) if fname.blank?  
end

I find this clunky and I have to put everyrhing in the validate method.  
Is  
there an easy to get what I want.

I tried this also validates\_presence\_of :fname , :message =\> ‘new  
message’  
still retains the fname.

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/how-to-change-the-error-message-easy-way/64759/2 "2006-08-15T14:18:46Z")

</div>

Assuming that you are using error\_messages\_for, why don’t you try  
overloading it to work the way you want. I would think that’s the  
easiest thing to do.

-carl

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [August 15, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/how-to-change-the-error-message-easy-way/64759/3 "2006-08-15T16:50:44Z")

</div>

I could try overloadin it but that causes the detachment issue.

For example if I have column name “fname” and I want the message to  
appear  
as 'First name" then in the “error\_messages\_for” method I need to put  
the  
code to replace 'fname" by “First name”.

Ideally I would like somethign like this.

validates\_presence\_of :fname, :show\_attribute\_name =\> ‘First name’.

Already we have a feature like validates\_presence\_of :fname , :message  
=\>  
‘new message’ . I guess I need to learn more ruby (& rails) in order to  
do  
this kind of hack. If someone already knows how to accomplish it easily  
please let me know.

-=-

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [August 15, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/how-to-change-the-error-message-easy-way/64759/4 "2006-08-15T17:11:50Z")

</div>

On 8/8/06, Neeraj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def validate  
> errors.add\_to\_base (“First Name can’t be blank”) if fname.blank?  
> end
> 
> I find this clunky and I have to put everyrhing in the validate method. Is  
> there an easy to get what I want.
> 
> I tried this also validates\_presence\_of :fname , :message =\> ‘new message’  
> still retains the fname.

I’ve always wondered this too. ☹

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [August 15, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/how-to-change-the-error-message-easy-way/64759/5 "2006-08-15T17:56:57Z")

</div>

I use the following to give me more flexibility with error messages:

[http://rubyforge.org/projects/custom-err-msg/](http://rubyforge.org/projects/custom-err-msg/)

works perfectly

Mike

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [August 15, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/how-to-change-the-error-message-easy-way/64759/6 "2006-08-15T17:08:50Z")

</div>

Hi,

How about Ruby-GetText-Package?

It’s an library for internationalization.  
But I think it’s fit for your purpose.

[http://www.yotabanana.com/hiki/?ruby-gettext-howto-rails](http://www.yotabanana.com/hiki/?ruby-gettext-howto-rails)

On Tue, 15 Aug 2006 10:47:48 -0400

---

<div class="post-metadata">

**Author:** ![nekapoor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea666f/32.png) [@nekapoor](https://www.ruby-forum.com/u/nekapoor)\
**Post date:** [August 15, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/how-to-change-the-error-message-easy-way/64759/7 "2006-08-15T18:02:57Z")

</div>

I use the plugin customm-err-msg.  
It allows you to omit the attribute name, but then you have to provide  
the whole message like

validates\_presence\_of :fname,  
:message =\> “^ First Name can’t be blank”

Dirk
