gpt4 book ai didi

ruby-on-rails - :path参数在命名空间中的浅路由不起作用

转载 作者:行者123 更新时间:2023-12-04 05:44:13 25 4
gpt4 key购买 nike

此路线设置

namespace :api, path: nil, except: [:new, :edit] do
resources :blogs do
resources :comments
end
end


给我这个,没关系。

GET    /blogs/:blog_id/comments(.:format)     api/comments#index
POST /blogs/:blog_id/comments(.:format) api/comments#create
GET /blogs/:blog_id/comments/:id(.:format) api/comments#show
PATCH /blogs/:blog_id/comments/:id(.:format) api/comments#update
DELETE /blogs/:blog_id/comments/:id(.:format) api/comments#destroy

GET /blogs(.:format) api/blogs#index
POST /blogs(.:format) api/blogs#create
GET /blogs/:id(.:format) api/blogs#show
PATCH /blogs/:id(.:format) api/blogs#update
DELETE /blogs/:id(.:format) api/blogs#destroy


但是当我在上面的设置中添加“ shallow:true”时

namespace :api, path: nil, except: [:new, :edit] do
resources :blogs, shallow: true do
resources :comments
end
end


出现不需要的路径“ / api”。

/api/blogs/:blog_id/comments(.:format) api/comments#index
/api/blogs/:blog_id/comments(.:format) api/comments#create
/api/comments/:id(.:format) api/comments#show
/api/comments/:id(.:format) api/comments#update
/api/comments/:id(.:format) api/comments#destroy

/blogs(.:format) api/blogs#index
/blogs(.:format) api/blogs#create
/api/blogs/:id(.:format) api/blogs#show
/api/blogs/:id(.:format) api/blogs#update
/api/blogs/:id(.:format) api/blogs#destroy


这是Rails 4中的预期行为吗?
我应该分别编写每个资源吗?

最佳答案

您需要指定shallow_path

namespace :api, path: nil, shallow_path: nil, except: [:new, :edit] do
resources :blogs, shallow: true do
resources :comments
end
end


给你这个:

       Prefix Verb   URI Pattern                        Controller#Action
blog_comments GET /blogs/:blog_id/comments(.:format) api/comments#index
POST /blogs/:blog_id/comments(.:format) api/comments#create
api_comment GET /comments/:id(.:format) api/comments#show
PATCH /comments/:id(.:format) api/comments#update
PUT /comments/:id(.:format) api/comments#update
DELETE /comments/:id(.:format) api/comments#destroy
api_blogs GET /blogs(.:format) api/blogs#index
POST /blogs(.:format) api/blogs#create
api_blog GET /blogs/:id(.:format) api/blogs#show
PATCH /blogs/:id(.:format) api/blogs#update
PUT /blogs/:id(.:format) api/blogs#update
DELETE /blogs/:id(.:format) api/blogs#destroy

关于ruby-on-rails - :path参数在命名空间中的浅路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21495197/

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