[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


class RegistrationsController < Devise::RegistrationsController


  def update_resource(resource, params)

❸ 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


★ 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)

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

❻ Add account_update to ApplicationController

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

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


★ 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