[RUBY] Edit user information with Devise

★ If you try to edit in the same way as new user registration ...

Current password can't be blank and an error message. .. ..

❶ Create a Controller to customize Devise

rails generate devise:controllers users

      create  app/controllers/users/confirmations_controller.rb
      create  app/controllers/users/passwords_controller.rb
      create  app/controllers/users/registrations_controller.rb
      create  app/controllers/users/sessions_controller.rb
      create  app/controllers/users/unlocks_controller.rb
      create  app/controllers/users/omniauth_callbacks_controller.rb

❷ Override update_resource method

#registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

  protected

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

❸ Added to routing

devise_for :users, controllers: { registrations: 'users/registrations' }

❹ Delete password input form

#views/devise/registrations/edit.html.Removed from erb

↓ The part that is easy to forget

425bd06c483f34fe28dc03a14824df75.png

★ It is said that the user's current_password is unknown ...

❺ Described using attr_accessor

(The value specified in the User model is handled as an instance variable)

#abridgement
attr_accessor :current_password

with_options format: { with: /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{6,}+\z/i } do
  validates :password, presence: true, on: :create
  validates :password_confirmation, presence: true, on: :create
end

❻ Add account_update to ApplicationController

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  private
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [#abridgement])
    devise_parameter_sanitizer.permit(:account_update, keys: [#abridgement])
  end

end

★ Devise A little understanding ...

Click here for this textbook

Recommended Posts

Edit user information with Devise
I can't edit user information with devise! ~ Update_resource Let's see here ~
devise user registration
[Rails] How to get the user information currently logged in with devise
[For beginners] Test devise user registration with RSpec
Handle devise with Rails
Implement devise edit page
How to update devise user information without a password
[Spring Boot] Get user information with Rest API (beginner)
When changing user information using devise Settings on the edit screen when the password is not saved
Memorandum [Rails] User authentication Devise
6 points to doubt when user registration is not possible with devise
Implement user management functionality using Devise
Create My Page with Rails devise
[Rails] Use devise to get information about the currently logged in user