I have implemented a login / logout function that uses devise on rails. I implemented it so that it will transition to the confirmation page when I log out I stumbled, so I'll write an article. If there is a better way, please write it in the comment section or edit request. : bow_tone2:
--Description in haml (gem'haml-rails') --devise is installed and you can log in
** respond_to_on_destroy ** of sessions_controller is described by default It was supposed to be transitioned to root_path. So delete the code here. You can move to the URL destination page by writing ** render'transition destination URL' ** there.
controllers/users/sessions_controller.rb
def destroy
signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
set_flash_message! :notice, :signed_out if signed_out
yield if block_given?
respond_to_on_destroy #Default description ← Code removed
render'Transition destination URL' # Describe the transition destination URL with render ← Enter the code
end
I created a destroy.html.haml file in users / destroy
.
python
%h1 You have logged out.
= link_to "Return to My Page", "/"
** sesstions: Customized sessions by adding "users / sessions" **! !!
python
devise_for :users, controllers: {
registrations: "users/registrations",
sessions: "users/sessions" #Customize
}
Customize the routing too! HTTP method **: delete **, URL, controller name # action name
devise_scope :user do
get 'profiles', to: 'users/registrations#new_profile'
post 'profiles', to: 'users/registrations#create_profile'
get 'addresses', to: 'users/registrations#new_address'
post 'addresses', to: 'users/registrations#create_address'
delete 'users/destroy', to: 'devise/sessions#destroy' #Postscript
end
Put the two together
routes.rb
devise_for :users, controllers: {
registrations: "users/registrations",
sessions: "users/sessions" #Customize
}
devise_scope :user do
get 'profiles', to: 'users/registrations#new_profile'
post 'profiles', to: 'users/registrations#create_profile'
get 'addresses', to: 'users/registrations#new_address'
post 'addresses', to: 'users/registrations#create_address'
delete 'users/destroy', to: 'devise/sessions#destroy' #Postscript
end
I felt that I still didn't understand MVC. It was a problem that I could solve immediately after thinking calmly. And I'm having a hard time writing Markdown. It took me a long time to write an article, so I have to be quick ... I will output more.
Shouma </ font>
Recommended Posts