1
我與7個的RESTful動作加上附加的「當前的」行動,它返回第一有源FOO記錄控制器:導軌3附加GET動作的RESTful控制器
class FooController < ApplicationController
def current
@user = User.find(params[:user_id])
@foo = @user.foos.where(:active => true).first
#use the Show View
respond_to do |format|
format.html { render :template => '/foos/show' }
end
end
#RESTful actions
...
end
foo的型號:belongs_to的用戶模型和用戶模型:has_many Foos。
如果我構造的路由這樣:
resources :users do
resources :foos do
member do
get :current
end
end
end
所得路徑爲 '/用戶/:USER_ID/FOOS /:ID'。顯然,我不想指定foo:id。
我也試過:
map.current_user_foo '/users/:user_id/current_foo', :controller => 'foos', :action => 'current'
resources :users do
resources :foos
end
得到的路線更像是我所期望的: '/用戶/:USER_ID/current_foo'。
當我嘗試使用這條路線,我得到一條錯誤:
ActiveRecord::RecordNotFound in FoosController#current
Couldn't find Foo without an ID
編輯
當我移動當前的行動應用控制器,一切正常。指定的路由必須與資源路由衝突。
/編輯
我缺少什麼?有沒有更好的路由方法?
有點不直觀,但它的工作 - 謝謝。任何想法如何處理的情況下,如果找不到匹配的foo?我可能想要顯示一個頁面,指出「該人沒有foo」,而不是回到最後一頁並顯示一條Flash消息。 – craig 2010-08-10 14:59:16
嗯,很難說,用戶如何選擇當前的foo?你在看用戶,然後點擊按鈕/按照鏈接查找分配給此用戶的當前foo的?如果是這樣,我會想回到請求頁面,並通知說,沒有發現foos會好起來的。 – Doon 2010-08-10 15:19:26