Forum: Ruby on Rails Validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mick (Guest)
on 2007-04-10 23:20
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
Alex W. (Guest)
on 2007-04-11 00:57
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])
    ...
Mick (Guest)
on 2007-04-11 03:51
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
Nathan Garza (Guest)
on 2007-05-06 08:52
(Received via mailing list)
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
Niels Meersschaert (Guest)
on 2007-05-06 22:58
(Received via mailing list)
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
This topic is locked and can not be replied to.