LondonGuy
LondonGuy

Reputation: 11098

Is it possible to update from an action/method other than the update action/method in Ruby on rails?

Is it possible to update from an action/method other than the update action/method? For example in my users controller I already have an update method for other parts of my users account.

I need a separate one for changing my users password. Is it possible to have something like this:

def another_method_to_update
  user = User.authenticate(current_user.email, params[:current_password])
  if user.update_attributes(params[:user])
    login user
    format.js   { render :js => "window.location = '#{settings_account_path}'" } 
    flash[:success] = "Password updated" 
  else
    format.js   { render :form_errors }

  end
end

Then have my change password form know to use that method to perform the update?

It has 3 fields: current password new password confirm new password

and I use ajax to show the form errors.

Kind regards

Upvotes: 0

Views: 56

Answers (1)

Arun Kumar Arjunan
Arun Kumar Arjunan

Reputation: 6857

Yes you can:

Add this in routes.rb:

resources :users do
  member do
    put :another_method_to_update
  end
end

In the view, you have to use the following URL:

another_method_to_update_user_path(@user)

Upvotes: 3

Related Questions