Search code examples
ruby-on-railsdevise

How to add calling action in update controller (edit user ) in devise gem?


I need to call function when user edit his page.

Here is my settings_controller.rb:

class SettingsController < ApplicationController
  def update
    @user = User.find(current_user.id)
    email_changed = @user.email != params[:user][:email]
    password_changed = !params[:user][:password].empty?
    successfully_updated = if email_changed or password_changed
      @user.update_with_password(params[:user])
    else
      @user.update_without_password(params[:user])
    end

    if successfully_updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      //need to call action here
    else
      render "edit"
    end
  end
end

I need to redirect user if successful update.

In my routes.rb:

devise_for :users, :controllers => {:registrations => 'registrations', :settings => 'settings'}

or I'm doing something wrong?


Solution

  • There is no settings route in devise_for.

    Look at RegistrationController in devise.

    You can overwrite this:

    class SettingsController < Devise::RegistrationsController
     def update
      @user = User.find(current_user.id)
      email_changed = @user.email != params[:user][:email]
      password_changed = !params[:user][:password].empty?
      successfully_updated = if email_changed or password_changed
        @user.update_with_password(params[:user])
      else
        @user.update_without_password(params[:user])
      end
    
      if successfully_updated
        # Sign in the user bypassing validation in case his password changed
        sign_in @user, :bypass => true
        //need to call action here
      else
        render "edit"
      end
     end
    end
    

    In routing:

    devise_for :users, :controllers => {:registrations => 'settings'}