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 2 years ago
- Reactions: 1
- Comments: 22 (2 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" %>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.what if I don’t wanna use turbo in my legacy code?
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.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
@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 confirmation@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.