question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Turbo can't be disabled for links with method: post/delete

See original GitHub issue

I’ve seen a few issues hovering around this issue (https://github.com/hotwired/turbo-rails/issues/31, https://github.com/hotwired/turbo/issues/45, https://github.com/hotwired/turbo/issues/74). But it seems that disabling turbo is not respected for links with non-GET methods.

I’m attempting to do the following:

link_to "Log Out", destroy_user_session_path, method: :delete, data: { turbo: false }

and the request is always processed as TURBO_STREAM:

Started DELETE "/users/sign_out" for ::1 at 2021-06-18 13:25:48 -0400
Processing by Users::SessionsController#destroy as TURBO_STREAM

If I use button_to instead of link_to, the disabling of turbo works properly.

Versions:

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:14
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
Erayd24commented, Oct 30, 2021

<%= button_to "Delete", url, method: :delete, class: 'button med-button', "data-turbo": false %>

This code works for me, though if I change it to a link_to or remove the data-turbo line it doesnt redirect. Just data-turbo = false or just button_to doesnt work either, it has to be both. And for whatever reason I can’t figure out, it was somehow ending up at my show method for pages instead of redirecting to the page I specified. I dont understand why it just ignores redirects. Doesn’t even get in to the desired controller action.

6reactions
tleishcommented, Aug 30, 2021

@fabianzwodrei - I think this is an issue with Devise and XHR requests, and creates problems with Turbo since it uses XHR. Similar issue reported on Stack **Overflow.

I believe this is because devise does not include status in redirect_to wi the session destroy (DELETE) action?

see: ActionController::Redirecting

If you are using XHR requests other than GET or POST and redirecting after the request then some browsers will follow the redirect using the original request method. This may lead to undesirable behavior such as a double DELETE. To work around this you can return a 303 See Other status code which will be followed using a GET request.

redirect_to posts_url, status: :see_other
redirect_to action: 'index', status: 303

I’m not sure any changes to turbo would solve this.

I think your options are:

  1. Use button_to (instead of link_to). This works since button_to creates a POST request (instead of DELETE) with params[:_method] = 'DELETE'.
  2. Configure devise sign out via GET with link_to (config.sign_out_via = :get)
  3. MonkeyPatch Devise controller to include status in redirect_to
Read more comments on GitHub >

github_iconTop Results From Across the Web

Link to delete and turbo drive disabled - Hotwire Discussion
I'v got turbo disabled app wide: import '@hotwired/turbo-rails' ... I still had method: :delete in link params, there should be only ...
Read more >
My Delete method is not working, after disabling "Turbo" in ...
I'm trying to run my 'Delete' method after disabling 'Turbo' but its not working, it keeps getting to my 'show.html.erb' file instead of ......
Read more >
Migrating from Rails UJS to Hotwire: Data Method, Confirm ...
Turbo now provides equivalents to Rails UJS data confirm, disable with, and method attributes that we can use.
Read more >
Turbo Drive | The Odin Project
You can disable Turbo Drive by adding data-turbo="false" directly on your links or on the parent containing them. For instance: <div data-turbo="false"> ...
Read more >
Turbo Drive Essentials - Akshay's Blog
By default, Turbo links send a GET request to the server. You can change this with the data-turbo-method attribute. <a href= ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found