Sinatra PUT 方法不起作用?

发布于 2024-09-15 18:34:14 字数 916 浏览 3 评论 0原文

由于某种原因,Sinatra 使用此 html 没有捕获我的“PUT”方法。有人可以帮我找出错误吗?当我在控制器中使用“后”操作时,它按照预期的方式工作......

<form method="post" action="/proposals/<%[email protected]%>/addItem">
<input type="hidden" name="_method" value="put"/>
  <div>
  <label for="item_id">Item list</label>
<select title="Item ID" id="item_id" name='item_id'>
  <%@items.each do |item|%>
    <option value="<%=item.id%>"><%=item.name%></option>
  <%end%>
</select>                                   
<input type="submit" value="Add"/></div>
<label for="new_item_name">Create new item</label>
<input type="text" id="new_item_name" name="new_item_name" />
<input type="submit" value="Create"/>
</form>

For some reason, my "PUT" method isn't caught by Sinatra using this html. Can someone help me spot the mistake? When I use a "post" action in my controller, it works just the way it is expected...

<form method="post" action="/proposals/<%[email protected]%>/addItem">
<input type="hidden" name="_method" value="put"/>
  <div>
  <label for="item_id">Item list</label>
<select title="Item ID" id="item_id" name='item_id'>
  <%@items.each do |item|%>
    <option value="<%=item.id%>"><%=item.name%></option>
  <%end%>
</select>                                   
<input type="submit" value="Add"/></div>
<label for="new_item_name">Create new item</label>
<input type="text" id="new_item_name" name="new_item_name" />
<input type="submit" value="Create"/>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一个人的旅程 2024-09-22 18:34:14

请务必在 config.ru 中包含 Rack::MethodOverride

use Rack::MethodOverride

Be sure to include Rack::MethodOverride in your config.ru:

use Rack::MethodOverride
浮生未歇 2024-09-22 18:34:14

这一切看起来都是正确的。看起来您要么写了错误的路由字符串,要么在 put 方法之前被另一个路由捕获。我对此很好奇,所以我编写了一个使用 put 方法的快速 Sinatra 应用程序,它确实以这种方式工作。

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'

get '/' do
  <<-eos
<html>
  <body>
    <form action="/putsomething" method="post">
      <input type="hidden" name="_method" value="put" />
      <input type="submit">
    </form>
  </body>
</html>
eos
end

put '/putsomething' do
  "You put something!"
end

That all looks correct. It looks like you either wrote the route string wrong, or it's being caught by another route before your put method. I was curious about this so I wrote up a quick Sinatra app that used a put method, and it does indeed work this way.

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'

get '/' do
  <<-eos
<html>
  <body>
    <form action="/putsomething" method="post">
      <input type="hidden" name="_method" value="put" />
      <input type="submit">
    </form>
  </body>
</html>
eos
end

put '/putsomething' do
  "You put something!"
end
紫竹語嫣☆ 2024-09-22 18:34:14

我刚刚遇到这个问题,上面的提示都没有帮助。我发现:

表单定义必须首先使用 action= ,然后使用 method=

正确的形式:

<form action="/putsomething" method="POST">
    <input type="hidden" name="_method" value="PUT" />
 ...
</form>

错误的形式:

<form method="POST" action="/putsomething">
    <input type="hidden" name="_method" value="PUT" />
 ...
</form>

第一个对我有用,第二个没有。
也许这有帮助。

I just run into this and none of the tips above helped. What I found:

The form definition has to come up first with the action= and second with method=

correct form:

<form action="/putsomething" method="POST">
    <input type="hidden" name="_method" value="PUT" />
 ...
</form>

wrong form:

<form method="POST" action="/putsomething">
    <input type="hidden" name="_method" value="PUT" />
 ...
</form>

The first worked for me, the second didn't.
Maybe this helps.

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