从 Sinatra 发送 DELETE 请求
我正在尝试开发一个 RESTful Sinatra 应用程序。现在,我知道如何响应删除请求,例如
delete '/user/:id' do |id|
#do something in the model
end
我感兴趣的是如何执行该方法。我无法拥有执行 DELETE 而不是 GET 的链接,可以吗?
到目前为止我发现的唯一解决方案是通过 jQuery 发送 DELETE 请求: 如何在 jQuery 中发送 PUT/DELETE 请求?
我尝试在 github 上查看不同的 RESTful Sinatra 项目,但我的 Ruby 知识可能仅限于了解他们是如何做到这一点的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将以下行放入您的代码中。
它将帮助您解释带有参数“
_method
”和值“delete
”的 post 方法。然后你可以写
Put following line in your code.
It will help you interpret post methods with parameter "
_method
" with value "delete
" as put.Then you can write
我认为这就像 Rails 的方式。您需要定义一个带有“delete”值的参数“_method”并将其添加到您的表单中。
当您使用此特定参数发布表单时,您会在 sinatra 中执行 DELETE 请求。
Like :
与 PUT 方法相同
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 :
It's the same with PUT method
另一种方法是使用 Curl:
Another way is to use Curl:
您还可以使用像这样的按钮触发删除路由
You can also trigger the delete route with a button like so
另请参阅使用 jQuery 调用 Sinatra 删除路由了解操作方法前端使用 jQuery 和 JSON,后端使用 Sinatra。
see also Call Sinatra delete route with jQuery for how to do this with jQuery and JSON at the front end and Sinatra on the back end.