2015-01-09 71 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