【问题标题】:Shallow routes inside a namespace with :path param not working名称空间内的浅路由:路径参数不起作用
【发布时间】:2014-02-01 07:00:29
【问题描述】:

此路线设置

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 中的预期行为? 我应该分别写每个资源吗?

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    您需要指定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
    

    【讨论】:

    • 谢谢!真的不知道这个!
    • 非常感谢,这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2018-03-25
    • 2012-03-10
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    相关资源
    最近更新 更多