正如您發現的,默認情況下,當您指定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