Transit to confirmation page when logging out with devise

Introduction

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:

version information

Prerequisites

--Description in haml (gem'haml-rails') --devise is installed and you can log in

Controller changes

** 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

Create View

I created a destroy.html.haml file in users / destroy.

python


%h1 You have logged out.
= link_to "Return to My Page", "/"

destroy.png

Controller customization

** sesstions: Customized sessions by adding "users / sessions" **! !!

python


  devise_for :users, controllers: {
    registrations: "users/registrations",
    sessions: "users/sessions" #Customize
  }

Routing customization

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

Summary

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

Transit to confirmation page when logging out with devise
Add an item when logging in with devise
6 points to doubt when user registration is not possible with devise
Create My Page with Rails devise
When I try to sign up with devise, it automatically redirects to root_path
How to handle sign-in errors with devise
[Laravel] How to deal with out of memory error when composer require [Docker]
[Rails] When transitioning to a page with link_to, move to the specified location on the page
How to get resource files out with spring-boot