gpt4 book ai didi

ruby-on-rails - 添加一个浅层资源作为另一个资源的成员

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:42 25 4
gpt4 key购买 nike

我正在使用 Rails 路由,我想要类似的东西

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

但我想在 params 中使用 :id 而不是在浅路径创建/索引中使用 :user_idThis is usually done by using member

You can leave out the :on option, this will create the same member route except that the resource id value will be available in params[:photo_id] instead of params[:id].

有谁知道为什么在 member 指令中完成浅化不能正常工作?

最佳答案

member 指令用于将成员路由添加到现有资源,而不是嵌套资源。我个人认为让 :id 引用索引中的父资源并创建操作是不清楚/令人困惑的,但如果您真的对在 Controller 中使用 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

关于ruby-on-rails - 添加一个浅层资源作为另一个资源的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27865099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com