Skip to content

Instantly share code, notes, and snippets.

@davidteren
Created October 31, 2022 17:42
Show Gist options
  • Save davidteren/c564b734ee0f9286b95d9bba104745e9 to your computer and use it in GitHub Desktop.
Save davidteren/c564b734ee0f9286b95d9bba104745e9 to your computer and use it in GitHub Desktop.
A simple solution for Rails 7 (Hotwire) & Devise sessions destroy
# In config/route.rb
Rails.application.routes.draw do
# other routes...
# Allows us to use link_to for session destroy
devise_scope :user do
get "/users/sign_out", as: "sign_out", to: "devise/sessions#destroy"
end
end
# In views
<% if user_signed_in? %>
<%= link_to "Sign Out", sign_out_path %>
<% else %>
<%= link_to "Sign In", new_user_session_path %>
<% end %>
@rubyhcm
Copy link

rubyhcm commented Aug 21, 2023

This works well

@davidteren
Copy link
Author

@rubyhcm, I was under the impression that this issue had been addressed in Devise 🤷‍♂️

@light-flight
Copy link

You can just change the config

Devise.setup do |config|
  config.sign_out_via = :get
end

@rubyhcm
Copy link

rubyhcm commented Apr 4, 2024

I was under the impression that this issue had been addressed in Devise

yeah, in rails 7. I use

<%= link_to "Sign out", destroy_user_session_path, data: { turbo_method: :delete } %>

@light-flight
Copy link

Right! That's how it should be.
But I'm having bugs when using it with data-turbo-permanent on my navbar where the logout button is located. After reloading the DOM is a mess 😄 (I'm also using <%= turbo_refreshes_with method: :morph, scroll: :preserve %>)
So I came up with data: { turbo: false } on a link, and moving to get request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment