Rails 5 - 嵌套路由 - 控制器的未初始化常量

Rails 5 - nested routes - uninitialised constants for controllers

我正在尝试弄清楚如何在 Rails 5 中嵌套路由(以便相关的控制器保持在一起。

我的控制器文件树设置为:

 app/controllers/users

在该文件夹中,我有以下控制器:

identities_controller.rb
app_roles_controller.rb

每个控制器都保存为:

class Users::IdentitiesController < ApplicationController
class Users::AppRolesController < ApplicationController

我的路线文件有:

resources :app_roles,
    :controllers => {
      :app_roles => 'users/app_roles'
    }

  devise_for :users,
             :controllers => {
                :sessions => 'users/sessions',
                :registrations => "users/registrations",
                :omniauth_callbacks => 'users/omniauth_callbacks'
              }

  resources :identities, 
    :controllers => {
        :identities => 'users/identities'
    }

  resources :users

在我的视图文件夹中,所有文件都是顶级文件。我不清楚是否需要像对控制器一样对它们进行分组。

当我保存所有这些并尝试导航到 http://localhost:3000/app_roles#index 时,我希望转到我的 app/views/app_roles/index。

相反,我收到一条错误消息:

app_roles
uninitialized constant AppRolesController

当我收集路线时,我得到:

rake routes | grep app_roles
                       app_roles GET      /app_roles(.:format)                    app_roles#index {:controllers=>{:app_roles=>"users/app_roles"}}
                                 POST     /app_roles(.:format)                    app_roles#create {:controllers=>{:app_roles=>"users/app_roles"}}
                    new_app_role GET      /app_roles/new(.:format)                app_roles#new {:controllers=>{:app_roles=>"users/app_roles"}}
                   edit_app_role GET      /app_roles/:id/edit(.:format)           app_roles#edit {:controllers=>{:app_roles=>"users/app_roles"}}
                        app_role GET      /app_roles/:id(.:format)                app_roles#show {:controllers=>{:app_roles=>"users/app_roles"}}
                                 PATCH    /app_roles/:id(.:format)                app_roles#update {:controllers=>{:app_roles=>"users/app_roles"}}
                                 PUT      /app_roles/:id(.:format)                app_roles#update {:controllers=>{:app_roles=>"users/app_roles"}}
                                 DELETE   /app_roles/:id(.:format)                app_roles#destroy {:controllers=>{:app_roles=>"users/app_roles"}}

对我来说,我认为这些路由表明 app_roles#index 应该通过 app/controllers/users/app_roles_controller.rb[=20 中的控制器转到 app/views/app_roles/index.html.erb =]

我对身份资源有同样的问题。

猜测 我尝试将 app/views/app_roles 文件夹移动到用户文件夹下(即 app/views/users),但是当我尝试转到 http://localhost:3000/app_roles#index 检查它是否有效时,我得到了同样的错误.

我还尝试将路由文件修改为:

resources :app_roles,
        :resources => {
          :app_roles => 'users/app_roles'
        }

我的意思是我将对 :controllers 的引用更改为 :resources。它没有用 - 我得到了同样的错误。

谁能看出我做错了什么?

路由到命名空间控制器

要将资源路由到 "namespaced" 控制器,您可以使用 module 选项:

resources :identities, module: :users

scope module:,这在声明多个资源时很有用:

scope module: :users do
  resources :app_roles, module: :users
  resources :identities, module: :users
end

这比手动指定控制器要简洁得多,后者实际上仅在您覆盖 Devise 等库或控制器和路由名称不一致时才执行。

这里的术语可能有些混乱。请记住,路由所做的唯一事情就是将传入请求与控制器进行匹配。它不会影响控制器的工作方式。

正在查看观看次数

To me, I think these routes show that app_roles#index should go to the app/views/app_roles/index.html.erb via the controller in app/controllers/users/app_roles_controller.rb

这不是它的工作原理。 Rails 根据控制器的嵌套查找视图 class。因此 rails 将在 views/users/indentities/.

中查找 Users::IdentitiesController 的视图

如果您想打破惯例,您可以显式呈现视图或 prepend/append the view paths。然而,在打破它们之前先了解这些约定,它们实际上非常聪明。

请注意,您的路线不会影响控制器查找视图的方式。这取决于模块嵌套——这就是对象的组成方式。并且与嵌套路由的概念完全无关。

"namespaces" 和嵌套路由

您生成的路由未嵌套。嵌套路由例如:

POST /users/:user_id/identities

清楚地描述了意图。要为 indentities 设置嵌套路由,您可以这样做:

resources :users, shallow: true do
  scope module: :users do
    resources :identities
  end
end

shallow: true 生成没有 users/:used_id/ 前缀的单独路由。

           Prefix Verb   URI Pattern                              Controller#Action
  user_identities GET    /users/:user_id/identities(.:format)     users/identities#index
                  POST   /users/:user_id/identities(.:format)     users/identities#create
new_user_identity GET    /users/:user_id/identities/new(.:format) users/identities#new
    edit_identity GET    /identities/:id/edit(.:format)           users/identities#edit
         identity GET    /identities/:id(.:format)                users/identities#show
                  PATCH  /identities/:id(.:format)                users/identities#update
                  PUT    /identities/:id(.:format)                users/identities#update
                  DELETE /identities/:id(.:format)                users/identities#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