Sinatra PUT 方法不起作用?
由于某种原因,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请务必在 config.ru 中包含
Rack::MethodOverride
:Be sure to include
Rack::MethodOverride
in your config.ru:这一切看起来都是正确的。看起来您要么写了错误的路由字符串,要么在 put 方法之前被另一个路由捕获。我对此很好奇,所以我编写了一个使用 put 方法的快速 Sinatra 应用程序,它确实以这种方式工作。
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.
我刚刚遇到这个问题,上面的提示都没有帮助。我发现:
表单定义必须首先使用 action= ,然后使用 method=
正确的形式:
错误的形式:
第一个对我有用,第二个没有。
也许这有帮助。
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:
wrong form:
The first worked for me, the second didn't.
Maybe this helps.