Topic: Update method not saving form

I can't seem to figure out why my update method isn't saving the form. It doesn't present any errors, it just silently fails. I am trying to save user profile data. Here is my code:

User controller:

def edit_profile
  @user = User.find(params[:id])
end
   
def update
  @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    flash[:note] = 'Cool!'
    redirect_to :action => 'welcome'
  else
    flash[:note] = 'uncool'
    redirect_to :action => 'edit_profile', :id => @user
  end
end

Edit Profile View
<%= start_form_tag :action=> 'update', :id => @user %>

<label for="user_hometown">Hometown</label><br/>
<%= text_field "user", "hometown", :size => 20 %><br/>

<label for="user_full_name">Full Name</label><br/>
<%= text_field "user", "full_name", :size => 20 %><br/>

<label for="user_full_name">login</label><br/>
<%= text_field "user", "login", :size => 20 %><br/>

<%= submit_tag "Save" %>

<%= end_form_tag %>


Any help would be much appreciated!
Andrew

Re: Update method not saving form

Where are you presenting flash[:note]?  Typically flash[:notice] is used, could that be the problem, or did you intentionally choose flash[:note]?

Re: Update method not saving form

What happens when you click save? Are you redirected to the welcome page, or the edit profile page?

Railscasts - Free Ruby on Rails Screencasts

Re: Update method not saving form

I intentionally used flash[:note]. I could easily switch it back to flash[:notice] though and the problem will still persist. I think it has something to do with what I am submitting not being valid? But, there are no errors....so it's very strange.

Re: Update method not saving form

I am redirected to the edit_profile page, which means that the data wasn't saved.

Re: Update method not saving form

The errors inside the model are lost upon redirect, so it is better to just render the template instead of redirecting:

render :action => 'edit_profile'

Also, make sure you are displaying the errors in the edit form:

<%= error_messages_for :user %>

Railscasts - Free Ruby on Rails Screencasts

Re: Update method not saving form

Thanks for the tips RyanB. I finally figured out what was wrong. It turns out that my Users table contains things that needed to be validated, ie. a hashed password, and password confirmation. Because of the authentication scheme I was using (http://www.aidanf.net/rails_user_authen … n_tutorial), since I wasn't passing a password, the update_attributes wasn't working. I ended up figuring it out after adding the line you suggested:

<%= error_messages_for :user %>

Then, in order to have conditional validation so that I didn't need to pass the password, I did this in the model:
validates_presence_of     :password,                   :if => :password_required?
validates_presence_of     :password_confirmation,      :if => :password_required?
validates_length_of       :password, :within => 5..40, :if => :password_required?
validates_confirmation_of :password,                   :if => :password_required?

protected
def password_required?
  hashed_password.nil? || !password.blank?
end


And, instead of using the update_attributes method, I am now using the save method (in the controller):
    def edit_profile
      @user = User.find(params[:id])
      return if @request.get?
         @user.attributes = params['user']
         if @user.save
           flash[:error] = nil
           flash[:note] = "Profile changes saved."
           redirect_to :action => 'edit_profile', :id => @user
         else
           flash[:error] = "Profile changes not saved, try again or please contact us."
           flash[:note] = nil
         end
    end

Again, thanks for the tips! And, if anyone else is having similar problems, I think I could do a better job explaining...
Andrew