2015-04-16 109 views
2

我試圖實現一個像「github.com/josevalim/inherited_resources」一樣的「àla github」網址。路徑與空路徑和幫手

我實現了我的目標與此代碼:

resources :users, :path => '', only: [] do 
    resources :projects, :path => '', only: [:show] 
end 

但現在,我無法找到一個方法來正確地重定向到這條路線。

當我運行在控制檯中rake routes,我只有

GET  /:user_id/:id(.:format)  projects#show 

,並沒有名字找到像我的舊users_project_path一個輔助方法。

我想念什麼?謝謝!

回答

1

我最近做了類似的事情,請嘗試以下

scope '/:user_id', as: :user do 
    resources :projects, path: '', only: [:show] 
end 

運行rake routes應該給你

user_project GET /:user_id/:id(.:format)  projects#show 

,這會給你適當的user_project_path(:user_id, :id)路由幫助。

0

由於這個原因,您正在刪除路徑,您可以獲取route_path,使用as來建立路由名稱。

resources :users, :path => '', only: [] do 
    get :projects, "/:user_id/:id", :to => "project#show", :as => :project_user 
end 

,或者如果你不想使用,你可以使用此代碼生成路徑:

url_for([@user, @project])