jer*_*emy 5 routing ruby-on-rails-3
在我的config/routes.rb中,我有:
post "portal_plan_document/update"
Run Code Online (Sandbox Code Playgroud)
rake路线证实了这一点:
$ rake routes
portal_plan_document_update POST /portal_plan_document/update(.:format) {:controller=>"portal_plan_document", :action=>"update"}
....
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我有:
<%= form_for @plan_doc,
:url => portal_plan_document_update_path,
:method => "POST", :remote => true do |f| %>
Run Code Online (Sandbox Code Playgroud)
在我的日志文件中,我看到:
Started POST "/portal_plan_document/update" for 127.0.0.1 at 2011-03-31 18:04:37 -0400
ActionController::RoutingError (No route matches "/portal_plan_document/update"):
Run Code Online (Sandbox Code Playgroud)
我迷失了,因为从这里做了什么.任何帮助将不胜感激!
我应该声明我使用的是Ruby 1.9.2和Rails 3.0.5.哦,我在更新routes.rb后重新启动了服务器(WebBrick w/rails服务器).
杰里米
弄清楚了!:)如果您有一个非空对象,rails假定您要更新该对象.即,使用'PUT'而不是'POST'
要完成'PUT',rails会在表单中放入一个隐藏的输入,"_method"="put".所以,它看起来像是一个POST,但rails将它视为PUT.
如果你真的想要更新一个对象(它看起来像你正在做什么),PUT会更好,你应该只将路由切换到PUT.
if(就像我一样),你正在做一些真正需要POST的东西(即,它不能安全地发送多次),你可以像这样编写form_for:
<%= form_for @plan_doc,
:url => portal_plan_document_update_path,
:html=>{:method => "POST"}, :remote => true do |f| %>
Run Code Online (Sandbox Code Playgroud)
确认,查看生成的HTML源代码,并确保隐藏的"_method"字段未设置为"put"
| 归档时间: |
|
| 查看次数: |
2187 次 |
| 最近记录: |