# Validation

**URL:** <https://www.ruby-forum.com/t/validation/91612>\
**Category:** Rails\
**Created:** [April 10, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/validation/91612 "2007-04-10T21:20:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 10, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/validation/91612/1 "2007-04-10T21:20:25Z")

</div>

Hi there,

I am trying to add some validation to protect against blank fields, but  
it does not seem to want to work, here is how I am trying to do it:

_view_

\<%= form\_tag :action =\> ‘update’ %\>

for opened

_controller_

def update  
if User.update(session[:user\_id],params[:user])  
flash[:message] = ‘User was successfully updated.’  
redirect\_to :action =\> ‘index’  
else  
flash[:message] = ‘An error occurred!’  
render :action =\> ‘edit’  
end  
end

_model_

validates\_presence\_of :username, :password, :firstname, :lastname,  
:email, :address1, :postcode, :city, :country, :on =\> :create  
validates\_presence\_of :firstname, :lastname, :email, :address1,  
:postcode, :city, :country, :on =\> :update

but it updates the form successfully even with blanks!

Am I missing something?

Cheers guys,  
Mick

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 10, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/validation/91612/2 "2007-04-10T22:57:55Z")

</div>

Mick wrote:

> Hi there,
> 
> I am trying to add some validation to protect against blank fields, but  
> it does not seem to want to work, here is how I am trying to do it:  
> def update  
> if User.update(session[:user\_id],params[:user])  
> flash[:message] = ‘User was successfully updated.’  
> redirect\_to :action =\> ‘index’  
> else  
> flash[:message] = ‘An error occurred!’  
> render :action =\> ‘edit’  
> end  
> end
> 
> but it updates the form successfully even with blanks!

From [http://caboo.se/doc/classes/ActiveRecord/Base.html#M006410](http://caboo.se/doc/classes/ActiveRecord/Base.html#M006410)

“If the save fails under validations, the unsaved object is still  
returned.”

So the update method does not let you know if the validations passed.  
Either way your model object is returned, and interpreted as true by the  
if.

This should fix it for you:

if User.find(session[:id]).update\_attributes(params[:user])  
…

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [April 11, 2007, 1:51am UTC](https://www.ruby-forum.com/t/validation/91612/3 "2007-04-11T01:51:10Z")

</div>

Alex W. wrote:

> Mick wrote:
> 
> > Hi there,
> > 
> > I am trying to add some validation to protect against blank fields, but  
> > it does not seem to want to work, here is how I am trying to do it:  
> > def update  
> > if User.update(session[:user\_id],params[:user])  
> > flash[:message] = ‘User was successfully updated.’  
> > redirect\_to :action =\> ‘index’  
> > else  
> > flash[:message] = ‘An error occurred!’  
> > render :action =\> ‘edit’  
> > end  
> > end
> > 
> > but it updates the form successfully even with blanks!
> 
> From [http://caboo.se/doc/classes/ActiveRecord/Base.html#M006410](http://caboo.se/doc/classes/ActiveRecord/Base.html#M006410)
> 
> “If the save fails under validations, the unsaved object is still  
> returned.”
> 
> So the update method does not let you know if the validations passed.  
> Either way your model object is returned, and interpreted as true by the  
> if.
> 
> This should fix it for you:
> 
> if User.find(session[:id]).update\_attributes(params[:user])  
> …

Hi, thanks for that it worked! However, the errors object (where the  
error messages that result from validates\_presence\_of) seems to be empty  
when accessed from the view? Any idea why this is getting lost?

The following returns nothing:

error\_messages\_for ‘user’

cheers,  
Mick

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [May 6, 2007, 6:52am UTC](https://www.ruby-forum.com/t/validation/91612/4 "2007-05-06T06:52:01Z")

</div>

I’m having a similar issue. Any one have any ideas?

–

Nathan Garza

AshLeaf Media | Director of Technology Innovations

* * *

[www.ashleafmedia.com](http://www.ashleafmedia.com) | removed\_email\_address@domain.invalid | 832.514.5726

---

<div class="post-metadata">

**Author:** ![mick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@mick](https://www.ruby-forum.com/u/mick)\
**Post date:** [May 6, 2007, 8:58pm UTC](https://www.ruby-forum.com/t/validation/91612/5 "2007-05-06T20:58:11Z")

</div>

You haven’t set an instance variable named @user.

Try this instead:

def update  
@user = User.find\_by\_id(session[:user\_id])  
@user.attributes = params[:user]  
flash[:message] = “User was successfully updated” and  
redirect\_to :action =\> :index and return if @user.save  
#If we didn’t return, we couldn’t save, so execute this alternate  
flash and render  
flash[:message] = “Unable to save changes”  
render :action =\> :edit  
end

Alternatively, you could gang this action into the edit action with  
if request.post?

Niels
