HTTP Requests in Sinatra

Jun 27, 2014 on Ruby Web Sinatra

In HTTP, there are four common request actions that are used by servers, GET, POST, PUT, and DELETE. This maps to CRUD, Create, Read, Update, and Delete. Unfortunately, the browsers can only send the GET and POST requests and not PUT and DELETE. To combat this, the server must fake the requests by using a hidden field to change the type of request in the browser. Consider the PUT request. The server file would have to have a GET method to a new page that has a form for updating, and that new page must have a form that uses the PUT method.

# server.rb 
get "/galleries/:id/edit" do
  @gallery = Gallery.find(params[:id])
  erb :edit_gallery

put "/galleries/:id" do
  gallery = Gallery.find(params[:id])
  redirect to("/galleries/#{params[:id]}")
<form action="/galleries/<%= %>" method="POST">
  <input type="hidden" name="_method" value="put">

While you have to do the same for the POST request as far as the server.rb file is concerned, you must add that second line in the edit_gallery.erb file. One thing of note is the name=“_method” value=“put” snippet. This is how Sinatra determines that you wanted it to be a PUT request and not a POST request and looks for a route that matches a put method.

The DELETE method is easier as you just need to create a delete input button as opposed to a creating a new GET request and new erb file. Just don’t forget to add the hidden input field.



Another comment.

Add a comment: