rails: Rails 7: link_to method: :delete not working
Steps to reproduce
Example:
<%= link_to 'Log out', destroy_user_session_path, method: :delete %>
The API docs for Rails 7 clearly state that you can use method: :delete, there are even some examples.
Expected behavior
A DELETE request is made.
Actual behavior
A GET request is made (i.e. method: :delete is ignored)
System configuration
Rails version: 7.0.1 Ruby version: 3.1
Related issues
This issue is similar, but not the same: https://github.com/rails/rails/issues/44170
This issue is about method: :delete which does not work.
The linked issue is about a wrong redirect when you use 'turbo-method': :delete.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 25 (5 by maintainers)
Sorry I bothered you. Everything works if I run:
$ rails importmap:install $ rails turbo:install stimulus:installI face the same problem with Rails 7.0.2.3 Non of the methods works:
to add if anyone else struggling. The following worked with me using the following set up
First do this
you may have to do this first if you get errors:
make sure you are using
data: { turbo_method: :delete }in yourlink_tomethod. I found thatmethod: :deletewas not workingexample of working code.
<%= link_to "Sign Out", destroy_user_session_path, data: { turbo_method: :delete }, class: "nav-link" %>I met the same problem and added this line into application.html.erb file to solve it . and it works fine under
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>+++<%= javascript_include_tag "turbo" ,type: "module" %>what if I don’t wanna use turbo in my legacy code?
I was able to resolve the issue following this other steps:
bundle add hotwire-railsref: hotwire-turbo-replacing-rails-ujsnow
link_tois working even with confirmation dialog.In my case the problem was that I had turbo Drive configured to work on a per element basis in my application.js, as shown here: https://turbo.hotwired.dev/handbook/drive but I was struggling to get the
data-turbo="true"mentioned in the documentation to work .Until I tried this, and it worked:
data: { turbo: true, turbo_method: :delete }Another option is to turn on turbo Drive globally, like this:In which case, the “turbo true” part is not necessary and this works:
data: { turbo_method: :delete }. Notice that using justmethod: :deletewill not work.@collimarco
could You tell me how to accomplish that with esbuild setup? I’ve tried passing this into application.js
but linkt_to with
method: :deleteanddata: { confirm: "confirmaton needed" }still wont sending get request without confirmationyes it worked , thank you … can some one explain what these lines do , after a quick search i came up to a conclusion that this had something to do with java script , bec delete req uses js , that caused the delete req to be handeled as a get req , why does this happen and how to prevent it happening again ?
In case this can help anyone else using devise: We encountered this problem on a log out link. We are using devise for auth, and not using turbo. Importing Rails ujs and specifying “delete” in the link did work, but we didn’t want to have to import Rails ujs just for this. We simply changed
config.sign_out_via = :deletetoconfig.sign_out_via = :getin our devise config.Аll is good on Rails 7.0.8 just use gem ‘turbo-rails’ in your gemfile use the import ‘@hotwired/turbo-rails’ in yaou application.js and write command yarn add @hotwired/turbo-rails or write in dependencies in to your package.json dependencies: { “@hotwired/turbo-rails”: “^8.0.0-beta.1” } it’s work for me. And use <%= link_to ‘Delete’, question_path(question), data: { turbo_method: :delete, turbo_confirm: ‘Are you sure?’ } %>. But i am create prod with -skip-hotwire. If you are have problems write the comments to me
I had the same issue and fixed it by adding
= javascript_importmap_tagsunder my= javascript_include_taginside the application.html.erb file.it worked for me
@michalwiacek
I think that you can still use Rails UJS… I also prefer that because I am not using Turbo.
Currently Rails UJS is not deprecated and I hope that they will keep it.