Let's look at how we delete a post. Currently we have:
<form method="post" action="/posts/<%= post.id %>" style='display: inline'>
<input type="submit" value="Delete" />
</form>
This issues an HTTP POST
. Since we changed to RESTful routes, we need to trigger a DELETE
HTTP verb here. However, since browsers natively only support HTTP GET
and POST
methods, we have to do something a bit tricky to use HTTP POST
to fake PUT
/PATCH
/DELETE
methods.
<form method="post" action="/posts/<%= post.id %>" style='display: inline'>
<input name= "_method" type="hidden" value="delete">
<input type="submit" value="Delete" />
</form>
On the server side, Rails is able to examine the "_method" value in the params
to restore the semantics of the HTTP request.
Take a look at your server log to make sure Rails interprets this as an HTTP DELETE
and also use a debugger to see it's routed to the destroy
action of the PostsController.