Getting sign out link_tos to work with Rails 7

How to get your sign out link_tos to work with Rails 7

With the release of Rails 7 we have seen yet another shift with how the frontend is handled. Rails 7 has moved away from Webpacker (thank goodness!) to using Turbo. When creating a new app I stumbled upon a new way of handling link_tos which I hadn’t seen before.

When I created my User using Devise I wanted to add a Sign Out button to my app, Devise was one of the first things I had set up in my app so I was running with a pretty vanilla app. When I clicked the Sign Out button I had created I got this error message: no route matches [get] "/users/sign_out", double checking my routes I could see in routes.rb I had devise_for :users and when running rails routes I had everything set up correctly routing wise.

The next thing I wanted to investigate was my link_to. When setting up older Rails projects and sign out links I had included rails_ujs and set my link_to like this: <%= link_to 'Sign Out', destroy_user_session_path, class: 'some-css-class', data: { method: :delete } %>. With Rails 7 using Turbo you can now set up Sign Out link_tos like this:

<%= link_to 'Sign Out', destroy_user_session_path, class: 'some-css-class', data: { turbo_method: :delete } %>

Note that method has now changed to turbo_method and you don’t need to set up any additional rails_ujs or jQuery to get link_tos to work like you would’ve in older versions of Rails.

If you want to add a confirm dialog to your delete you’ll have to pass turbo_confirm instead of confirm, like this:

<%= link_to 'Delete', article_path(article), data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } %>

Recent posts View all

Freelancing

Getting the most out of your agency

Here are some tips based on years of working with, for, and as an agency on how to get the most out of any work you do.

VS Code Web Dev

Select multiple lines in VS Code

How to select multiple lines to edit at once within VS Code