从 Sinatra 发送 DELETE 请求

发布于 2024-10-20 01:48:02 字数 452 浏览 4 评论 0原文

我正在尝试开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不气馁 2024-10-27 01:48:02

将以下行放入您的代码中。

use Rack::MethodOverride

它将帮助您解释带有参数“_method”和值“delete”的 post 方法。
然后你可以写

delete '/user/:id' do |id|

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|
甜妞爱困 2024-10-27 01:48:02

我认为这就像 Rails 的方式。您需要定义一个带有“delete”值的参数“_method”并将其添加到您的表单中。

当您使用此特定参数发布表单时,您会在 sinatra 中执行 DELETE 请求。

Like :

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

与 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 :

<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

顾铮苏瑾 2024-10-27 01:48:02

另一种方法是使用 Curl:

curl -X DELETE http://host/user/1

Another way is to use Curl:

curl -X DELETE http://host/user/1
静赏你的温柔 2024-10-27 01:48:02
  %form{:action => "/note/delete/#{@note.id}", :method => "post"}
    %input{:type => 'submit', :name=> "_method", :value => 'delete', :class => 'button'}

您还可以使用像这样的按钮触发删除路由

  %form{:action => "/note/delete/#{@note.id}", :method => "post"}
    %input{:type => 'submit', :name=> "_method", :value => 'delete', :class => 'button'}

You can also trigger the delete route with a button like so

缪败 2024-10-27 01:48:02

另请参阅使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文