Justin Davis
Justin Davis

Reputation: 15

<a> link method: :delete HTML

I am relatively new to programing and just wondering if there is a way to have something like = link_to "sign out", destroy_user_session_path, method: :delete, but using an <a> link.

EDIT

Let me clarify, I did use = link_to "sign out", destroy_user_session_path, method: :delete in my code (i'm using haml), but it's still doing a get request. This is in the dropdown menu and it works normally if placed on the actual page. Is there any reason for this, or is rails just being stupid?

Upvotes: 0

Views: 294

Answers (3)

Prasanth_Rubyist
Prasanth_Rubyist

Reputation: 458

Here are the default rails helper methods for making links, please refer below link. you'll get a good understanding of link_to

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

Upvotes: 0

vidur punj
vidur punj

Reputation: 5871

<%= link_to('sign out', destroy_user_session_path, method: :delete) %>

if you have generated devise controller, please do it like :

rails d devise:controllers users

instead of:

rails d devise:controllers user

Upvotes: 0

Chris Heald
Chris Heald

Reputation: 62648

link_to will create an <a> element. It's just a Rails helper method which creates <a> elements, but with the added benefits of being able to use things like the Rails route helpers easily, and attaching extra behaviors (such as non-GET verbs). Normally, clicking an <a> element will cause the browser to execute a GET request, but a delete is not idempotent, so it shouldn't ever be done with a GET request.

Rails' link_to helper helps you around this by allowing you to specify method: :delete, which wires up additional Javascript click handlers for the element, which will cause it to perform a POST via Javascript rather than just letting the browser make a GET request when the link is clicked. It's still an <a> element and may be styled and presented as such, but when clicked, it'll perform a form post using the DELETE verb, rather than performing a simple GET request.

Upvotes: 1

Related Questions