Nonstandard HTTP Verbs

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.

Hi! I'm LSBot. I'm here to help you understand this chapter content with fast , focused answers. Any code from the editor will be automatically included with your question.

Ask me about concepts, examples, or anything you'd like clarified from this chapter. I can explain complex topics, provide examples, or help connect ideas.

Switch to Deep Dive mode if you want comprehensive answers across the entire curriculum. Responses may take longer, but I'll draw on the entire Launch School curriculum to give you a fuller picture.

Want to know more? Refer to the LSBot User Guide .