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)

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" %>

I was able to resolve the issue following this other steps:

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

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.

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

@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

@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.