[Rails] [Devise] Edit profile without password

In the default state of Devise, you need to enter the password, confirmation password, and current password to edit the profile.

Since it is very troublesome for the user to enter the password to edit the profile, we have made it possible to edit the profile information without entering the password.

I will post it as an oblivion record.

environment

ruby 2.7.2 rails 5.2.4.4

Fixed devise routing

config/routes.rb


Rails.application.routes.draw do
  devise_for :users, #Add a comma to the line here
    controllers: { registrations: 'registrations' } #Add line here

Create registrations_controller.rb

Create registrations_controller.rb inapp / controllers /. Add the following code to registrations_controller.rb

app/controllers/registrations_controller.rb


class RegistrationsController < Devise::RegistrationsController

  protected

  def update_resource(resource, params)
    resource.update_without_current_password(params)
  end
end

Implement update_without_current_password method in User model

Add the following code to app / models / user.rb

app/models/user.rb


class User < ApplicationRecord

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

# ============add to====================
  def update_without_current_password(params, *options)
    params.delete(:current_password)

    if params[:password].blank? && params[:password_confirmation].blank?
      params.delete(:password)
      params.delete(:password_confirmation)
    end

    result = update_attributes(params, *options)
    clean_up_passwords
    result
  end
# ====================================

end

Now you can edit your profile information without having to enter your password. You can also edit the password.

Change the redirect destination after editing the profile

If it is the default of Devise, after editing the profile, ** it will transition to the top page **. After editing my profile, I want to redirect to the profile page of the logged-in ** user **, so I added the following code to registrations_controller.rb.

app/controllers/registrations_controller.rb


class RegistrationsController < Devise::RegistrationsController

  protected

  def update_resource(resource, params)
    resource.update_without_current_password(params)
  end

# ============add to====================
  def after_update_path_for(resource)
    user_path(resource)
  end
# ====================================

end

The after_update_path_for method is a method provided by Devise, and you can specify which path to transition to after updating the account.

that's all!


Referenced site https://easyramble.com/user-account-update-without-password-on-devise.html

Recommended Posts

[Rails] [Devise] Edit profile without password
[Rails] devise
[Rails] Introducing devise
How to update user edits in Rails Devise without entering a password
rails + devise + devise_token_auth
[Rails] devise helper method
How to update devise user information without a password
[Rails] Customize devise validation
Handle devise with Rails
[Rails] Cancel / change the default password validation of devise
[Rails] devise introduction method
[Rails] How to edit and customize devise view and controller
[rails] About devise defaults
Implement devise edit page
[Rails] Introduction of devise Basics
What is Rails gem devise?
[Rails] gem devise installation flow
[Rails] How to install devise
[Rails] Add column to devise
[rails] error during devise installation
Memorandum [Rails] User authentication Devise
Edit user information with Devise
[rails] Edit of view page created by devise is not reflected
[Rails 5] How to display the password change screen when using devise