Rails 5在父级上浅路由,但对于子级不浅

时间:2017-01-12 21:19:14

标签: ruby-on-rails routing ruby-on-rails-5 nested-routes

我有关于rails中浅层路由的问题。我有一组嵌套3个级别的路由users有许多portfolios有许多displays。我想要做的是为我的投资组合设置一条浅路线,但是对于显示器有详细的路由。我尝试过shallow: false,但似乎没有做任何事情

# config/routes.rb
resources :users do
  resources :portfolios, shallow: true do
    resources :displays #shallow: false
  end
end

对于我的用户和投资组合,这是我想要的方式

#users routes
/users      #index
/users/:id  #show

#portfolios routes
/users/:user_id/portfolios #index
/portfolios/:id            #show

但是,从这里开始,我希望详细的显示能够从浅薄的投资组合中锁定

#desired display routes
/portfolios/:portfolio_id/displays     #index
/portfolios/:portfolio_id/displays/:id #show

#actual display routes
/portfolios/:portfolio_id/displays     #index
/displays/:id                          #show

有没有办法以这种方式利用浅层配置?

1 个答案:

答案 0 :(得分:1)

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  resources :users do
    resources :portfolios, shallow: true 
  end
  resources :users, only: [] do
    resources :portfolios, only: [] do
      resources :displays
    end
  end
end

以下是路线

                     Prefix Verb   URI Pattern                                                          Controller#Action
            user_portfolios GET    /users/:user_id/portfolios(.:format)                                 portfolios#index
                            POST   /users/:user_id/portfolios(.:format)                                 portfolios#create
         new_user_portfolio GET    /users/:user_id/portfolios/new(.:format)                             portfolios#new
             edit_portfolio GET    /portfolios/:id/edit(.:format)                                       portfolios#edit
                  portfolio GET    /portfolios/:id(.:format)                                            portfolios#show
                            PATCH  /portfolios/:id(.:format)                                            portfolios#update
                            PUT    /portfolios/:id(.:format)                                            portfolios#update
                            DELETE /portfolios/:id(.:format)                                            portfolios#destroy
                      users GET    /users(.:format)                                                     users#index
                            POST   /users(.:format)                                                     users#create
                   new_user GET    /users/new(.:format)                                                 users#new
                  edit_user GET    /users/:id/edit(.:format)                                            users#edit
                       user GET    /users/:id(.:format)                                                 users#show
                            PATCH  /users/:id(.:format)                                                 users#update
                            PUT    /users/:id(.:format)                                                 users#update
                            DELETE /users/:id(.:format)                                                 users#destroy
    user_portfolio_displays GET    /users/:user_id/portfolios/:portfolio_id/displays(.:format)          displays#index
                            POST   /users/:user_id/portfolios/:portfolio_id/displays(.:format)          displays#create
 new_user_portfolio_display GET    /users/:user_id/portfolios/:portfolio_id/displays/new(.:format)      displays#new
edit_user_portfolio_display GET    /users/:user_id/portfolios/:portfolio_id/displays/:id/edit(.:format) displays#edit
     user_portfolio_display GET    /users/:user_id/portfolios/:portfolio_id/displays/:id(.:format)      displays#show
                            PATCH  /users/:user_id/portfolios/:portfolio_id/displays/:id(.:format)      displays#update
                            PUT    /users/:user_id/portfolios/:portfolio_id/displays/:id(.:format)      displays#update
                            DELETE /users/:user_id/portfolios/:portfolio_id/displays/:id(.:format)      displays#destroy