Sending a DELETE request from Sinatra
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|
Send DELETE request to an API endpoint using Nestful Sinatra
[Solved]: I sent a DELETE request through Nestful using the following commands:
delete '/events/:id' do
request = Nestful::Request.new(endpoint, options)
request.method = 'delete'
response = request.execute
end
In the above piece of code, endpoint
is "https://api.teamsnap.com/v3/events/EVENT_ID" and options
is a hash which contains "Content-Type" and "Authorization" headers.
Sinatra - delete action
You need this component in your middleware pipeline use Rack::MethodOverride
Another way seems to be put set :method_override, true
in your Sinatra::Base
class
See this also
How can I delete a file in Sinatra after it has been sent via send_file?
Unfortunately there is no any callbacks when you use send_file. Common solution here is to use cron tasks to clean temp files
Can I simulate a PUT or a DELETE request using a POST request with Sinatra?
Please read the document and find the description of method_override
. The mechanism is identical as Rails. If your browser doesn't support PUT
& DELETE
, just send a extra parameter named _method
with value PUT
or DELETE
.
Notice that in Modular application (your class inherits Sinatra::Base
), method_override
is disabled by default. You need to enable it manually.
Sinatra view doesn't refresh after some delete requests
There is reloading a page and there is redirecting to a page.
When you use the redirect
method of Sinatra it responds with a 302 or 303 status code.
# @see https://github.com/sinatra/sinatra/blob/v2.0.0/lib/sinatra/base.rb#L271
# Halt processing and redirect to the URI provided.
def redirect(uri, *args)
if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
status 303
else
status 302
end
# According to RFC 2616 section 14.30, "the field value consists of a
# single absolute URI"
response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?)
halt(*args)
end
By calling redirect "/"
you are asking the browser to redirect to "/", which would normally trigger a fresh page. However, you're already on that page, so the jQuery code is receiving a redirect and you're asking for a reload via location.reload()
upon success. Success is generally indicated by responding with a 2xx code, so I'd posit that the redirect means the success handler isn't firing, and because the browser realises you're already on the page the redirect has asked for it doesn't attempt a redirect either and you end up with no fresh data.
The route should probably look more like this:
delete '/delete/:record_id' do
id = get_record_id
delete_record id
halt 200 # think about handling errors too
end
The success handler should now fire. Moral of the story, be explicit in what you want to do, don't rely on side effects (like a redirect sharing some properties of a reload) to do what you want.
Related Topics
Omniauth Facebook Expired Token Error
Is There a Ruby Http Client Library with a Response Cache
Elasticsearch Terms Aggregation by Entire Field
How to Detect the End of a Method Chain in Ruby
Custom Rails Configuration Section
Rendering a JSON Object of a Join-Model and Its Associated Models
Elegantly Selecting Attributes from Has_Many :Through Join Models in Rails
Dynamically Generate Scopes in Rails Models
Checking If a String Is Valid JSON Before Trying to Parse It
Get Time Object at Start of Day in a Particular Time Zone
Ruby, Tor and Net::Http::Proxy
How to Install a Gem Globally Without Sudo Using Rbenv
Require Lib in Rspec with Ruby 1.9.2 Brings "No Such File to Load"
Activerecord Association Select Counts for Included Records
Ruby Rest-Client File Upload as Multipart Form Data with Basic Authenticaion