Search code examples
ruby-on-railsrestruby-on-rails-pluginsrestful-authentication

Update User Info with restful_authentication plugin in Rails?


I want to give the users the ability to change their account info with restful_authentication plugin in rails.

I added this two methods to my users controller:

  def edit
    @user = User.find(params[:id])
  end

  def update
      @user = User.find(params[:id])

      # Only update password when necessary
      params[:user].delete(:password) if params[:user][:password].blank?

      respond_to do |format|
        if @user.update_attributes(params[:user])
          flash[:notice] = 'User was successfully updated.'
          format.html { redirect_to(@user) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
        end
      end
    end

Also, I copied new.html.erb to edit.html.erb. Considering that resources are already defined in routes.rb I was expecting it to work easily, but somehow when I click the save button it calls the create method, instead of update, using a POST HTTP request. Immediately after that it automatically logs out from the session.

Any ideas?


Solution

  • Could it be possible that the selected user could not be found in your edit method? Then @user is empty and your form thinks that you are creating a new user.

    Try to add debugging output in the edit method, like this:

    def edit
      @user = User.find(params[:id])
      logger.debug "Found user = #{@user.inspect}"
    end
    

    And check if your log messages shows that the user is found or not.