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

Ruby on rails Rails 5-嵌套路由-控制器的未初始化常量,ruby-on-rails,ruby,routes,nested-routes,Ruby On Rails,Ruby,Routes,Nested Routes,我试图弄清楚如何在Rails 5中嵌套路由(以便相关的控制器保持在一起) 我已将控制器文件树设置为: app/controllers/users 在该文件夹中,我有以下控制器: identities_controller.rb app_roles_controller.rb 这些控制器中的每一个都保存为: class Users::IdentitiesController < ApplicationController class Users::AppRolesController

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

我已将控制器文件树设置为:

 app/controllers/users
在该文件夹中,我有以下控制器:

identities_controller.rb
app_roles_controller.rb
这些控制器中的每一个都保存为:

class Users::IdentitiesController < ApplicationController
class Users::AppRolesController < ApplicationController
在“我的视图”文件夹中,所有文件都是顶级文件。我不清楚是否需要按照与控制器相同的方式对它们进行分组

当我保存所有这些并尝试导航到时,我希望转到我的应用程序/视图/应用程序角色/索引

相反,我得到的错误是:

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中的控制器进入app/views/app#roles/index.html.erb

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

猜测 我尝试将app/views/app_roles文件夹移动到users文件夹下(即app/views/users),但当我尝试转到以检查其是否有效时,出现了相同的错误

我还尝试将routes文件修改为:

resources :app_roles,
        :resources => {
          :app_roles => 'users/app_roles'
        }
我的意思是,我将引用改为:controllers,改为:resources。它不起作用-我得到了相同的错误

有人知道我做错了什么吗?

路由到名称空间的控制器 要将资源路由到“名称空间”控制器,可以使用
模块
选项:

resources :identities, module: :users
范围模块:
,在声明多个资源时非常有用:

scope module: :users do
  resources :app_roles, module: :users
  resources :identities, module: :users
end
这比手动指定控制器要干净得多,而手动指定控制器实际上只有在重写像Desive这样的库或控制器和路由名称不对齐时才能完成

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

查看视图 对我来说,我认为这些路线表明应用程序角色索引应该转到 app/views/app_roles/index.html.erb通过中的控制器 app/controllers/users/app\u roles\u controller.rb

这不是它的工作方式。Rails根据控制器类的嵌套查找视图。因此Rails将在
视图/用户/标识/
中查找
用户::标识控制器的视图

如果你想打破惯例,你可以显式地呈现视图或视图。但是,在打破惯例之前先学习一下惯例,它们实际上是相当聪明的

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

“名称空间”和嵌套路由 您正在生成的路由不是嵌套的。嵌套路由可能是,例如:

POST /users/:user_id/identities
这清楚地描述了意图。要为
标识设置嵌套路由,您需要执行以下操作:

resources :users, shallow: true do
  scope module: :users do
    resources :identities
  end
end
shallow:true
生成不带
users/:used\u 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

也许可以尝试将您的文件放在一个有名称空间的文件夹中,即
app/controllers/users/.rb
。它们位于:app/controllers/users您是否也有
app/controllers/user\u controller
?它可以只包含
class users controller
i-do-我的users controller是顶级的(未嵌套在app/controllers/users文件夹中)您好,Max,非常感谢您的解释。当我进行浅层嵌套时,这是否意味着我应该将“标识视图”文件夹嵌套在“用户视图”文件夹中?我想这样做是为了使文件结构整洁,但我无法直观地理解“视图”文件结构中浅层开关关闭了什么。路由不需要ot影响控制器查找视图的方式。它们只调用匹配的控制器#Action视图查找由控制器类的声明方式决定。如果它包装在模块中,rails将在查找中使用该模块。好的,但是当你说“包装在模块中”时,你是指我编写嵌套控制器的方式吗适合“wrap in module”格式?我的嵌套控制器的开头行是:class Users::identiescontroller 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