turbo: Turbo can't be disabled for links with method: post/delete
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:
- Ruby Gems:
- rails (6.1.3.2)
- turbo-rails (0.5.9)
- NPMs:
- @hotwired/turbo-rails: 7.0.0-beta.5
- @rails/ujs: 6.1.3
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 15
- Comments: 20 (2 by maintainers)
Commits related to this issue
- Use data-turbo-method instead of data-method Refer to the following issue: https://github.com/hotwired/turbo/issues/288 — committed to satoryu/sealion-rails by satoryu 3 years ago
<%= 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.
@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
statusinredirect_towi the session destroy (DELETE) action?see: ActionController::Redirecting
I’m not sure any changes to turbo would solve this.
I think your options are:
params[:_method] = 'DELETE'.config.sign_out_via = :get)statusin redirect_toSame goes for all other non get requests (i.e.
patch,delete)I’d imagine a few people will get caught out with Rails 7 like myself, clear explanation in the docs here.
<%= link_to %>for anything other than get requests<%= button_to %>for post requestsThe team added support for links and POST|PUT|PATCH|DELETE in v7.0.0-beta.6
see: https://github.com/hotwired/turbo/releases/tag/v7.0.0-beta.6
I pulled down 7.0.0-beta.6 and it looks like the rails server is still processing the actions as TURBO_STREAM.
For me works only forcing redirect to html format in sessions controller, like below:
It’s work for me 100% <%= link_to “Logout”, destroy_user_session_path, method: “DELETE”, data: { “turbo-method”: “DELETE” } %>
Using 7.0.0-beta.6: My sign-out link for devise is working with the
data-turbo-methodbut its reponse comes via TUBRO_STREAM and the following redirection is misguided by keeping the DELETE method.<%= link_to "Logout", destroy_user_session_path, method: "DELETE", data: {"turbo-method": "DELETE"}%>log:
@efatsi did the new release fixed this for you?
In my case, it does work, but it’s still processing the delete as turbo_stream. Which caused my flash message to disappear (it redirects twice).