[RUBY] [Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing

It is a memorandum of a fledgling engineer. This is the first post. Also, it is inefficient to check again when "How do you do that?" Someday, so I will leave it here. Please report any mistakes.

The default is the root path

In my environment, root redirects to the top page, so I want both to transition to the user's details screen. Preparations such as the introduction of devise will be omitted.

1. Creating a controller

$ rails g devise:controllers users

Of the several created controllers, the one I will use this time is registrations_controller.rb

Add here

controllers/users/registrations_controller.rb


class Users::RegistrationsController < Devise::RegistrationsController
  
  #Add the following
  protected

  def after_sign_up_path_for(resource)
    flash[:notice] = 'Completion of registration! You can register your profile image and self-introduction text from the edit button.'
    user_path(resource) #Specify the redirect destination path here
  end

  def after_update_path_for(resource)
    user_path(resource) #Specify the redirect destination path here
  end
end

Now the controller is ready

2. Edit routes.rb

To the created controller class Users::RegistrationsController < Devise::RegistrationsController As you can see, we created a new Users :: RegistrationsController that inherits the Devise :: RegistrationsController originally used in devise, so we will reflect the additional settings by writing here. be able to. (I didn't realize that it was inherited at all, and I was impatient to say, "Do I have to touch all the commented out new actions and update actions to make a new controller?", But it's okay as it is. was) However, since Users :: RegistrationsController cannot be used as it is, It is necessary to clearly describe and use the following.

routes.rb


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

Now you can use the controller you created. You should be redirected to the path you specified.

Setting to skip password input when editing

If it is the default, you will be prompted to enter the password you are setting even if you try to update it. Allows you to update without entering a password. As before, add the following to the controller.

controllers/users/registrations_controller.rb


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

After that, it is ok if you delete the form part of current_password in view.

Articles that I referred to (almost as they are rather than references) https://qiita.com/Tatty/items/a9759755e562ac4693ec https://note.com/ruquia7/n/n4838547cb054 https://qiita.com/machamp/items/f6a7b003fcda3f04094a

Recommended Posts

[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing
[Rails 5] How to display the password change screen when using devise
[Rails] How to get the URL of the transition source and redirect
[Rails] How to introduce kaminari with Slim and change the design
[Rails] How to change the page title of the browser for each page
[Ruby on Rails] How to log in with only your name and password using the gem devise
How to update user edits in Rails Devise without entering a password
[Rails] How to get the user information currently logged in with devise
[Rails] How to create a table, add a column, and change the column type
[Rails 6] Change redirect destination at the time of new registration / login by devise
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
How to redirect after user login with Spring-security
[rails] After option useful when you want to change the order of DB columns
[rails devise] How to transition users who are not logged in to the login page
When I created a user my page, the user editing function of devise became strange.
How to update devise user information without a password
[Ruby on Rails] How to change the column name
[Rails] How to change the column name of the table
[Rails] Cancel / change the default password validation of devise
[Rails] How to edit and customize devise view and controller
How to solve the problem when the value is not sent when the form is disabled in rails and sent
[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
[Rails] Processing after adding a column to the devise table
[Rails] When the layout change of devise is not reflected
How to run React and Rails on the same server
When you want to change the MySQL password of docker-compose
Implement user registration function and corporate registration function separately in Rails devise
Method definition location Summary of how to check When defined in the project and Rails / Gem
[Rails] How to install devise
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
[Rails] Add a confirmation screen and a completion screen to devise membership registration.
[Caution !!] Precautions when converting Rails devise and view files to haml
6 points to doubt when user registration is not possible with devise
How to write the view when Vue is introduced in Rails?
[Rails] How to solve the problem that the default image is overwritten when editing without uploading the image [Active Storage]