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.
ruby 2.7.2 rails 5.2.4.4
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
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
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.
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