Home > OS >  Rails 7 - link_to with method :delete still performs GET request
Rails 7 - link_to with method :delete still performs GET request

Time:12-26

I am trying to get this link to work, performing a DELETE request:

<%= link_to "Sign Out", destroy_user_session_path, method: :delete %>

However when I click on it, my browser still performs a GET request (which fails for obvious reasons):

screenshot of browser network console

I have read on multiple other forum posts, that this might have something to do with jquery not being included. They mentioned you would need to un-comment a line in app/javascript/application.js, however mine is pretty empty:

// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"

These forum posts were also quite old, so I suspect something has changed in the meantime.

CodePudding user response:

As suggested here, the following will suffice:

<%= link_to "Sign Out", destroy_user_session_path, data: { "turbo-method": :delete } %>

I have tested this in my project and it seems to work fine. Thanks also to @alexts, you basically figured this out too, however the comment on GitHub even eliminated the double-request.

CodePudding user response:

Since it is a problem with including jQuery and jquery_ujs can you try to add those two lines in your app/javascript/application.js

import "require jquery"
import "require jquery_ujs"
  • Related