Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 7: link_to method: :delete not working #44185

Closed
collimarco opened this issue Jan 17, 2022 · 24 comments
Closed

Rails 7: link_to method: :delete not working #44185

collimarco opened this issue Jan 17, 2022 · 24 comments

Comments

@collimarco
Copy link

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: #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.

@willyjie
Copy link

because rails-ujs is remove :)

@rafaelfranca
Copy link
Member

method: :delete really doesn't work. It needs to be turbo_method: :delete if you are using Rails default stack. See https://guides.rubyonrails.org/getting_started.html#deleting-an-article

@collimarco
Copy link
Author

@rafaelfranca In any case the API reference is wrong and needs to be updated, that's why I opened this issue:

https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

In my original post:

The API docs for Rails 7 clearly state that you can use method: :delete, there are even some examples.

@rafaelfranca
Copy link
Member

@byteg
Copy link

byteg commented Mar 18, 2022

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

@byteg
Copy link

byteg commented Mar 18, 2022

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

@ghost
Copy link

ghost commented Mar 23, 2022

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

I confirm, it's work also if I run that.
Big thank

@Abdelrahman-kamall
Copy link

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 ?

@LukeSparky
Copy link

LukeSparky commented Apr 20, 2022

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

@michalwiacek
Copy link

what if I don't wanna use turbo in my legacy code?

@collimarco
Copy link
Author

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

@michalwiacek
Copy link

michalwiacek commented Aug 9, 2022

@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

@iVieL
Copy link

iVieL commented Sep 1, 2022

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

Hey there, I tried those steps, but rails 7.0.3.1 looks to don't have importmap:install task, just these

image

the link_to with any of these combinations doesn't work yet:

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

data: { turbo_method: :delete }
data: { method: :delete }
method: :delete

as an additional info, I'm kind of new with RoR.
The tutorial that I'm following is using:

  • hamlit
  • hamlit-rails
  • simple_form

and was created like this:
rails new project_name --css bootstrap -T --database=postgresql

any other clue or tip to solve the issue?

@iVieL
Copy link

iVieL commented Sep 1, 2022

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

now link_to is working even with confirmation dialog.

@Debadattajena080
Copy link

it worked for me

@curtisbarnard
Copy link

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 was following the free code camp Rails course and came upon this issue. Doing the above seems to have fixed it. Thanks!

@peoplenamed
Copy link

I had the same issue and fixed it by adding = javascript_importmap_tags under my = javascript_include_tag inside the application.html.erb file.

quipcode added a commit to quipcode/ruby-alpha-blog that referenced this issue Nov 17, 2022
…ails importmap:install $ rails turbo:install stimulus:install as per rails/rails#44185
@ali-p-q
Copy link

ali-p-q commented Dec 26, 2022

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.

@lyc4n
Copy link

lyc4n commented Apr 7, 2023

Encountered the issue in 7.0.4.3 but it turned out I had to run ./bin/dev because rails server alone does not compile the assets. I got so much used to the old version. Hope this helps.

@mamboZero
Copy link

i can confirm this work! tq

@hansonfox
Copy 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" %>

@Ivan-Sizyh
Copy link

Ivan-Sizyh commented Dec 14, 2023

А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

@DogAndCode
Copy 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" %>

This works for me. Why is that?

@Lilaro
Copy link

Lilaro commented Jan 16, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests