出于某种原因,我的“PUT”方法没有被Sinatra使用这个html捕获。有人能帮我发现错误吗?当我在我的控制器中使用“post”动作时,它的工作方式与预期的一样......
<form method="post" action="/proposals/<%=@proposal.id%>/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>
答案 0 :(得分:14)
请务必在config.ru中加入Rack::MethodOverride
:
use Rack::MethodOverride
答案 1 :(得分:10)
一切看起来都是正确的。看起来你要么写错了路由字符串,要么在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
答案 2 :(得分:0)
我刚刚碰到这个,上面的提示都没有帮助。我找到了什么:
表单定义必须首先使用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>
第一个为我工作,第二个没有。 也许这有帮助。