Search code examples
htmlrestsinatrasinatra-activerecord

Sending a delete request to the url in an anchor tag


How would I send a delete request to the specified URL (in the href attribute).

<a href="/apartments/<%= @apartment.id %>">Delete</a>

So far, I have tried method = "delete" as an attribute in the anchor tag. I can get it to work by making a button like so,

  <form action = "/apartments/<%= @apartment.id %>" method = "post">
    <input type="hidden" name="_method" value="delete" />
    <button type="submit">Delete</button>
  </form>

However, I'd prefer to keep it a link so that it is uniform with Edit, View Details, and other similar actions that appear with this particular item.

I'm using Sinatra.

Thanks!


Solution

  • You cannot use an anchor tag (at least without JS). Links are meant to be idempotent.