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.
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.
$ 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
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.
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