Ask LSBot

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.

This conversation with LSBot is temporary. Sign up for free to save your conversations with LSBot.

Hi! I'm LSBot. I'm here to help you understand this chapter content with fast, focused answers.

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.

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

This conversation with LSBot is temporary. Sign up for free to save your conversations with LSBot.

Hi! I'm LSBot. I'm here to help you think through this exercise by providing hints and guidance, without giving away the solution.

You can ask me about your approach, request clarification on the problem, or seek help when you feel stuck.

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