DEV Community

Stephane Paquet
Stephane Paquet

Posted on

Rails 7 + Devise + Log out

A Quick and Dirty way to get the redirection working on Logout when using Devise in a Rails 7 app.

You probably have noticed that most of the redirects in Devise a somehow broken. This is due to the way Turbo interfere with them as it catches the 200 status code.

There are already a lot of posts explaining how to patch this while waiting for an official release. But, if your main issue is about the "redirect on log out" not working, then you can easily fix this.

Instead of calling destroy_user_session_path in a link_to, call it inside of a button_to

Here is the full code:

<%= button_to(
        "Log Out",
        destroy_user_session_path,
        method: :delete
      ) %>
Enter fullscreen mode Exit fullscreen mode

And Voilà! This simple hack enables your users to be redirected to the root_path of your app in seconds.

Top comments (5)

Collapse
 
andyfoster profile image
Andy • Edited

Chim Kan has a way to add Turbo to the link_to method and this worked for me.

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

chimkan.com/rails-7-and-devise-iss...

Collapse
 
kkamikazi profile image
kkamikazi

The problem on my app is that the redirect will try a DELETE request on the root_path too.

Collapse
 
damuz91 profile image
David Muñoz

Check with rails routes if the route you are trying to use as logout is the proper one. Also check in the rails server console what is the output and what is the route being requested when you click the button.

Collapse
 
clint326 profile image
Clint Laskowski

IMHO this is a huge problem. Using button_to doesn't really solve the problem. Is anyone working on this? Has anyone found a work-around.

Collapse
 
spaquet profile image
Stephane Paquet

Well, the team behind Devise answered to our interrogation about the state of the project here https://www.reddit.com/r/rails/comments/sy376j/whats_going_on_with_devise_for_rails_7/?utm_source=share&utm_medium=web2x&context=3

but, at the time of writing, we are all waiting for the proposed changes to be approved and and a new version being released.