Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a DELETE request from Sinatra

I am trying to develop a RESTful Sinatra application. Now, I know how to respond to a delete request with something like

delete '/user/:id' do |id|
   #do something in the model
end

What I am interested in is how do I get to execute that method. I can't have link that does a DELETE instead of a GET, can I?

The only solution I found so far is sending a DELETE request via jQuery: How to send a PUT/DELETE request in jQuery?

I tried looking into different RESTful Sinatra projects on github but my Ruby knowledge is probably to limited to get how they are doing it.

like image 302
sebastiangeiger Avatar asked Mar 02 '11 10:03

sebastiangeiger


2 Answers

Put following line in your code.

use Rack::MethodOverride

It will help you interpret post methods with parameter "_method" with value "delete" as put. Then you can write

delete '/user/:id' do |id|
like image 138
Zimbabao Avatar answered Nov 04 '22 10:11

Zimbabao


I thinks it's like the Rails way. You need define a params '_method' with 'delete' value and add it on your form.

When you POST you form with this particular params, you do a DELETE request in sinatra.

Like :

<form action="/search" method="post">
  <div style="margin:0;padding:0">
    <input name="_method" type="hidden" value="delete" />
  </div>
</form>

It's the same with PUT method

like image 26
shingara Avatar answered Nov 04 '22 09:11

shingara