2015-01-09 70 views
2

我使用的軌道路由的一員,我想有一些像添加淺資源作爲另一個資源

resources :user 
    member do 
    resources :comments, shallow: true 
    end 
end 

# To get the following routes 
get /users/:id/comments (index) 
post /users/:id/comments (create) 
get /comments/:id (show) 
put /comments/:id (update) 
delete /comments/:id (destroy) 

然而變淺不工作,我有以下途徑(不提的是,:id用戶和評論是相互矛盾的)

get /users/:id/comments 
post /users/:id/comments 
get /users/:id/comments/:id 
put /users/:id/comments/:id 
delete /users/:id/comments/:id 

我知道,平時做的推薦方法是

resources :user 
    resources :comments, shallow: true 
end 

# To get the following routes 
get /users/:user_id/comments 
post /users/:user_id/comments 
get /comments/:id 
put /comments/:id 
delete /comments/:id 

但我想要:idparams而不是:user_id淺層路由創建/索引。 This is usually done by using member

你可以離開了:上選項,這將創建不同的是值的資源ID將在 PARAMS提供相同的成員 路線[:photo_id]代替PARAMS [:ID]。

有沒有人知道爲什麼在member指令內完成淺化後不能正常工作?

回答

0

member指令用於將成員路由添加到現有資源,而不是嵌套資源。我個人認爲有:id指父資源索引和創建操作還不清楚/混亂,但如果你真的通過在控制器使用user_id打擾,你可以設置你以下面的方式途徑

resources :user do 
    resources :comments, shallow: true, except: [:index, :create, :new, :edit] 
end 

get '/users/:id/comments', to: 'comments#index' 
post '/users/:id/comments', to: 'comments#create' 

這會給你以下路線

GET /users/:id/comments 
POST /users/:id/comments 
GET /comments/:id 
PUT /comments/:id 
DELETE /comments/:id