HeretoLearn
HeretoLearn

Reputation: 7434

Rails using REST resource and being able to delete without a form

Rails simulates a DELETE on a resource by using POST and a hidden field in the Form. But that makes it a must to use a form to delete a record. However if I wanted to delete a row via a Anchor tag 'Delete' displayed at the end of the row how would I achieve the same? So say I am displaying people records and my rows looks like:

||First Name|| Last Name||
||Foo || Bar || Delete
||Baz || Foo || Delete
...

And on the delete click I want to route to /people/:id/destroy without using a Form and Hidden field?

Upvotes: 1

Views: 353

Answers (1)

Kyle Decot
Kyle Decot

Reputation: 20815

Rails' link_to method supports the GET, POST, PUT, and DELETE HTTP verbs. For example, you could do something like:

link_to "Delete", person_path(@person), method: :delete, confirm: "Are you sure?"

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

Upvotes: 2

Related Questions