# Error Message is not coming in Validations

**URL:** <https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858>\
**Category:** rubyonrails-talk\
**Created:** [May 27, 2008, 1:36pm UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858 "2008-05-27T13:36:53Z")\
**Posts on this page:** 11\
**Page:** 1

<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:** [May 27, 2008, 1:36pm UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/1 "2008-05-27T13:36:53Z")

</div>

Hi,

I am using validations in my website .the problem is that the error message is not coming while giving wrong inputs or while validations.But the validations are working fine,what I mean that while giving wrong inputs the page will not redirect to next page.

I am using the validations in the models and also used

\<%= error\_messages\_for 'model\_name'%\> in my rhtml file

Thanks and Regards Divyesh Kumar

---

<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:** [May 27, 2008, 1:43pm UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/2 "2008-05-27T13:43:20Z")

</div>

Are you sure the validations are working and generating errors on the object? If the redirects do not work, the problem is most likely in the controller. What are you doing there?

---

<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:** [May 29, 2008, 9:51am UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/3 "2008-05-29T09:51:24Z")

</div>

Yes the validation are working fine ,if I am giving right inputs redirect is working fine.

---

<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:** [May 29, 2008, 9:53am UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/4 "2008-05-29T09:53:54Z")

</div>

Divyesh Kumar wrote:

> Yes the validation are working fine ,if I am giving right inputs redirect is working fine.

if params[:user] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user = User.find(session[:user].id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if user.update\_attributes(params[:user]) #&& ((params[:password]== "" && params[:password\_confirmation] == "") || (params[:password] != "" && params[:password\_confirmation] != "")) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:user] = user &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if params[:picture] != "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User.upload\_picture(params,session[:user]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> "show" , :id =\> session[:user].id &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice]="Field marked with asterisk(\*) are necessary or might be wrong" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> "edit\_user\_info" , :id =\> session[:user].id

As I am using flash to print to print the error message.The problem is this is not the googd idea for me,I want complete error message for that.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 29, 2008, 10:00am UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/5 "2008-05-29T10:00:07Z")

</div>

If you want to get the validation messages you can't use redirect\_to (at least not without some extra work) since that throws away the user with errors. You need to use render

Fred

---

<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:** [May 29, 2008, 10:16am UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/6 "2008-05-29T10:16:41Z")

</div>

Frederick Cheung wrote:

> If you want to get the validation messages you can't use redirect\_to (at least not without some extra work) since that throws away the user with errors. You need to use render
> 
> Fred

Ok, I will take care of this suggestion into my account.

I added like this:

if params[:user] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user = User.find(session[:user].id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if user.update\_attributes(params[:user]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:user] = user &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if params[:picture] != "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User.upload\_picture(params,session[:user]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 29, 2008, 10:23am UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/7 "2008-05-29T10:23:58Z")

</div>

> Frederick Cheung wrote:
> 
> > If you want to get the validation messages you can't use redirect\_to (at least not without some extra work) since that throws away the  
> > user with errors. You need to use render
> > 
> > Fred
> 
> Ok, I will take care of this suggestion into my account.

You also need to set @user to the user your editing or  
error\_messages\_for 'user' won't know what to display

Fred

---

<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:** [May 29, 2008, 10:37am UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/8 "2008-05-29T10:37:29Z")

</div>

Frederick Cheung wrote:

> > > Fred
> > 
> > Ok, I will take care of this suggestion into my account.
> 
> You also need to set @user to the user your editing or error\_messages\_for 'user' won't know what to display
> 
> Fred

This error is cuming after I made changes in controller as well as rhtml file NameError in User#edit\_user\_info

Showing app/views/user/edit\_user\_info.rhtml where line #36 raised:

`@@user' is not allowed as an instance variable name

Extracted source (around line #36):

33: \<form action="edit\_user\_info" class="genericFormClass" method="post" enctype="multipart/form-data"\> 34: 35: 36: \<%= error\_messages\_for '@user'%\>

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 29, 2008, 10:41am UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/9 "2008-05-29T10:41:51Z")

</div>

> Frederick Cheung wrote:
> 
> > > > Fred
> > > 
> > > Ok, I will take care of this suggestion into my account.
> > 
> > You also need to set @user to the user your editing or error\_messages\_for 'user' won't know what to display
> > 
> > Fred
> 
> This error is cuming after I made changes in controller as well as rhtml file NameError in User#edit\_user\_info

That's because it should be \<%= error\_messages\_for 'user'%\> (check the docs)

Fred

---

<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:** [May 29, 2008, 10:48am UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/10 "2008-05-29T10:48:24Z")

</div>

Frederick Cheung wrote:

> > > Fred
> > 
> > This error is cuming after I made changes in controller as well as  
> > rhtml file NameError in User#edit\_user\_info
> 
> That's because it should be \<%= error\_messages\_for 'user'%\> (check the docs)
> 
> Fred

you are right.I am making the changes but still it is not cuming

U also suggested to change sumthing before,here is ur statement:

You also need to set @user to the user your editing or error\_messages\_for 'user' won't know what to display

what is that??

Thanks Divyesh

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 29, 2008, 10:50am UTC](https://discuss.rubyonrails.org/t/error-message-is-not-coming-in-validations/29858/11 "2008-05-29T10:50:25Z")

</div>

> Frederick Cheung wrote:
> 
> > > > Fred
> > > 
> > > This error is cuming after I made changes in controller as well as rhtml file NameError in User#edit\_user\_info
> > 
> > That's because it should be \<%= error\_messages\_for 'user'%\> (check  
> > the docs)
> > 
> > Fred
> 
> you are right.I am making the changes but still it is not cuming
> 
> U also suggested to change sumthing before,here is ur statement:
> 
> You also need to set @user to the user your editing or error\_messages\_for 'user' won't know what to display

In the controller you need to use @user in order for the view to be  
able to do anything with it in the view you use error\_messages\_for 'user' This should be covered by any introduction to rails type tutorial, you  
might find you save a lot of time by doing a little reading rather  
than stabbing around in the dark.

Fred
