Rails 路由:具有根路由的命名空间,如何处理单数/复数 URI 和控制器命名空间?

问题描述 投票:0回答:1

我想创建以下路线:

Prefix              Verb   URI Pattern               Controller#Action
configuration       GET    /configuration            configurations#index
configuration_team  GET    /configuration/teams/:id  configurations/teams#show

但是拥有单数“配置”URI 和复数“configurations#index”控制器命名空间似乎不合常规。

到目前为止我所得到的是:

  • a
    get '/configuration', to: 'configurations#index', as: :configuraiton
  • 和一个
    namespace :configuration { ... }

遗憾的是,这为控制器创建了一个单一的

configuration/
命名空间。

有方便的方法吗?或者我应该坚持约定(如果我是对的,

/configurations
URI 和命名空间)

ruby-on-rails routes
1个回答
0
投票

这可能不是最好的解决方案,但我最终得到了:

get '/configuration', to: 'configuraitons#index', as: :configuration

scope :configuration do
  resources :teams, only: %i[show], controller: 'configurations/teams', as: :configuration_team
end

创建以下路线:

Prefix              Verb   URI Pattern               Controller#Action
configuration       GET    /configuration            configurations#index
configuration_team  GET    /configuration/teams/:id  configurations/teams#show
© www.soinside.com 2019 - 2024. All rights reserved.