使用rails button_to
帮助程序,我尝试在 Controller 中运行update
方法。我需要手动设置正在更新的对象的ID。我认为我拥有的代码应该是正确的,但 Rails 一直试图将 ID 作为路线的一部分。
在 View 中:
button_to ">", :controller=>'people', :action=>'update', 'person'=>{:team=>team_leader.team}, :id=>currently_viewing_person
在 Controller 中:
def update
@person = Person.find(params[:id])
#...rest of method
end
Controller 更新方法永远不会执行。网络浏览器上的错误是:
Unknown action
No action responded to 3. Actions: create, index, new, search, show, and update
“3”是currently_viewing_person
的值
传递 :id
以便更新可以从 params[:id]
中提取的正确方法是什么
最佳答案
button_to
默认使用 POST。要进行更新,您需要 PUT,因此请传入该方法以及其他参数:
button_to ">",
{ :controller=>'people', :action=>'update',
'person'=>{:team=>team_leader.team},
:id=>currently_viewing_person },
:method => :put
请注意,该方法必须作为单独的哈希传入
关于ruby-on-rails - 铁路通行证 :id with params,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3252161/