我正在使用rails routing,我想要像
这样的东西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)
然而,shallowing不起作用,我有以下路线(更不用说用户的: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
但是我希望在浅绿色路径create / index上的:id
而不是params
中使用:user_id
。 This is usually done by using member
您可以省略:on选项,这将创建相同的成员 路由,但资源ID值可用 params [:photo_id]而不是params [:id]。
是否有人知道为什么在member
指令内完成后,shallowing无法正常工作?
答案 0 :(得分: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