Reputation: 339
In my rails app, I have a following link:
<%= link_to("Log out", logout_path, method: :delete)%>
And in my routes I added:
delete '/logout', to: 'sessions#destroy'
However, when I click on the Log out link, I get the following routing error:
No route matches [GET] "/logout"
It seems that even I specified "method: :delete", the "link_to" function still issues a get request instead of a delete request. How do I send a delete request with ruby on rails?
My application.js looks like this:
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .
//= require jquery
//= require jquery_ujs
//= require bootstrap
Upvotes: 1
Views: 1316
Reputation: 12203
This usually occurs due to a missing JS dependency in application.js
.
If you're using Rails 5, add:
//= require rails-ujs
And for earlier versions ensure you have:
//= require jquery
//= require jquery_ujs
In there.
The reason for this is that a little Rails magic 'fakes' the delete request as most browsers don't support this. It will add data-method="delete"
to your HTML tag, allowing your application to recognise the request as it should be, and process as DELETE
(even though it's actually sent as a GET
request).
Also, ensure you're loading said Javascript via the following line in your layout:
<%= javascript_include_tag "application" %>
Let me know how you get on or if you have any questions - hope this helps.
Upvotes: 3