Shallow routes inside a namespace with :path param not working

最后都变了- 提交于 2019-12-11 02:33:12

问题


This routes setting

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

gave me this and it's ok.

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

But when I add "shallow: true" to the setting above

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

an unwanted path '/api' comes up.

/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

Is this an expected behavior still in Rails 4? Should I write each resources respectively?


回答1:


You need to specify the shallow_path:

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

Gives you this:

       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


来源:https://stackoverflow.com/questions/21495197/shallow-routes-inside-a-namespace-with-path-param-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!