praaveen
Published in

praaveen

Rails link_to method: GETing when it should DELETE

Issue :
Most browsers don’t actually support the DELETE verb, so Rails fakes it by modifying the HTML it generates. Rails tacks on a HTML5 attribute called data-method and sets it to "delete". So when a user clicks on the link, it is actually issued as a GET request, but the data-method attribute allows for some Rails magic and means your routing code should recognize it as a DELETE request.

View file

<%= link_to 'Sign Out', destroy_user_session_path, method: :delete, class: 'logins-link navbar-link-right' %
>

rails log

Started GET "/users/sign_out" for ::1 at 2022-06-17 12:44:08 +0530
Processing by UsersController#show as HTML
Parameters: {"id"=>"sign_out"}

Note:

It’s working perfectly if I use button _to instead of link_to
<%= button_to 'Delete', article_path(article), method: :delete %>

Solution :

As browsers don’t support the DELETE verb, Rails creates a workaround for this problem by simulating a DELETE request through a standard GET or POST. The way method: :delete works is with a JavaScript handler for all links with data-method="delete" which modifies the request so Rails processes it as a DELETE. This JavaScript handler is provided by the jquery_ujs library.

There are several things that could have gone wrong:

  1. Make sure you have both jquery and jquery_ujs included in your application.js. Without both nothing will process the data attributes.
  2. Make sure the link in question really has the method: :delete option specified.
  3. Make sure for some reason you haven’t stopped the event propagation of the link in question, like so for example:
$( 'a' ).click( function( event ) {
event.stopPropagation()
})

As this would prevent the jquery_ujs handlers from being executed and the request will never be modified and will remain just a standard GET.

a. application.js

//= require jquery
//= require jquery_ujs

Note that you may need to add these lines above any import statements within application.js.

b. And also make sure to includ below line in your layout file

<%= javascript_include_tag "application" %>

c. view file

<%= link_to 'Sign Out', destroy_user_session_path, method: :delete, class: 'logins-link navbar-link-right' %
>

d. Now rails Log

Started DELETE "/users/sign_out" for ::1 at 2022-06-17 12:47:46 +0530
Processing by Users::SessionsController#destroy as HTML
Parameters: {"authenticity_token"=>"XXXxKlhWdsfsdf435345345sfdsfsd"}

Note :

If you don’t want to include JQuery, switching link_to > button_to will fix this issue.

Reference :

--

--

--

Technical blog

Recommended from Medium

How I conquered writer's block with Google Assistant + Google Docs

Developer Week Conference 2018 Summary in Oakland

Zombie Swarm!

Please report this story for content violation.

Hide Data in Images — Encrypted Steganography With Flutter

How to reset your old smart TV’s pin number — Panasonic Viera

Difference between Single Value Lookup , Multi Value Lookup & Joins.

Community Imbalance Theory

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Praaveen Vr

Praaveen Vr

Ruby on Rails Freelancer

More from Medium

Routing | Rails

How to Reload a Method While in a Pry Session in Ruby

Object Orientation in Ruby. Basics.

Rails Today