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:
- Make sure you have both
jquery
andjquery_ujs
included in yourapplication.js
. Without both nothing will process the data attributes. - Make sure the link in question really has the
method: :delete
option specified. - 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 :