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 (3)

Collapse
andyfoster profile image
Andy • Edited on

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

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.

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.