正如您发现的,默认情况下,当您指定resources :things
时,用于创建新事物的POST路径为/things
。下面是输出为rake routes
:
things GET /things(.:format) {:action=>"index", :controller=>"things"}
POST /things(.:format) {:action=>"create", :controller=>"things"}
new_thing GET /things/new(.:format) {:action=>"new", :controller=>"things"}
edit_thing GET /things/:id/edit(.:format) {:action=>"edit", :controller=>"things"}
thing GET /things/:id(.:format) {:action=>"show", :controller=>"things"}
PUT /things/:id(.:format) {:action=>"update", :controller=>"things"}
DELETE /things/:id(.:format) {:action=>"destroy", :controller=>"things"}
这听起来像你想要更多的东西是这样的:
create_things POST /things/new(.:format) {:action=>"create", :controller=>"things"}
things GET /things(.:format) {:action=>"index", :controller=>"things"}
new_thing GET /things/new(.:format) {:action=>"new", :controller=>"things"}
edit_thing GET /things/:id/edit(.:format) {:action=>"edit", :controller=>"things"}
thing GET /things/:id(.:format) {:action=>"show", :controller=>"things"}
PUT /things/:id(.:format) {:action=>"update", :controller=>"things"}
DELETE /things/:id(.:format) {:action=>"destroy", :controller=>"things"}
虽然不建议,你可以得到这个结果与下列路线:
resources :things, :except => [ :create ] do
post "create" => "things#create", :as => :create, :path => 'new', :on => :collection
end
您还需要修改表单以使其POST到正确的路径。
所有这些都说了,你在你的问题中的URL描述听起来不对。你列出以下内容:提交新thing
(在/things/new
提交表单),
- 从
/things/new
的网址更改为/things
- 点击回提示重新提交表格后
- 刷新显示
things#index
这是而不是我在自己的Rails 3应用程序中遇到的功能。相反,我发现:提交新thing
(在/things/new
提交表单),
- 从
/things/new
的网址更改为/things
(这是相同的)
- 点击回将用户回后非 -submitted形式(重后不请求)
- 刷新提示重新提交表单(如预期在我看来)
我对Rails核心的投诉完全一样,但你比我更好地表达了它:)好问题。 – Andrew 2011-04-03 03:35:09