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)

Commits related to this issue

Most upvoted comments

Sorry I bothered you. Everything works if I run: $ rails importmap:install $ rails turbo:install stimulus:install

I face the same problem with Rails 7.0.2.3 Non of the methods works:

  1. data: { turbo_method: :delete }
  2. data: { method: :delete }
  3. method: :delete

to add if anyone else struggling. The following worked with me using the following set up

ruby "3.0.0"
gem "rails", "~> 7.0.2", ">= 7.0.2.3"
gem 'devise', '~> 4.8', '>= 4.8.1'

First do this

$ rails importmap:install $ rails turbo:install stimulus:install

you may have to do this first if you get errors:

$ rails redis:install

make sure you are using data: { turbo_method: :delete } in your link_to method. I found that method: :delete was not working

example 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:

now link_to is 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:

import { Turbo } from "@hotwired/turbo-rails" 
Turbo.session.drive = true

In which case, the “turbo true” part is not necessary and this works: data: { turbo_method: :delete }. Notice that using just method: :delete will not work.

@collimarco

could You tell me how to accomplish that with esbuild setup? I’ve tried passing this into application.js

import Rails from "@rails/ujs";
Rails.start();

but linkt_to with method: :delete and data: { confirm: "confirmaton needed" } still wont sending get request without confirmation

Sorry I bothered you. Everything works if I run: $ rails importmap:install $ rails turbo:install stimulus:install

yes 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 = :delete to config.sign_out_via = :get in 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_tags under my = javascript_include_tag inside 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.