Validation


#1

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


#2

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

“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])


#3

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

“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


#4

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

Nathan Garza

AshLeaf Media | Director of Technology Innovations


www.ashleafmedia.com | removed_email_address@domain.invalid | 832.514.5726


#5

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