Rails 4:在文件夹中对控制器进行分组 [英] Rails 4: Grouping Controllers in a folder
问题描述
我想将我所有的控制器及其视图分组在文件夹中,例如在admin"下
I would like to group all my controllers and their views in folders for example under "admin"
这就是我所做的:
(1) 我把所有的 controllers
移到一个文件夹 admin
下(2)我把所有的views
都移到了admin
This what I did:
(1) I moved all the controllers
under a folder admin
(2) I moved all the views
under a folder admin
(3) 我在 Rails 指南中读到我应该这样做:
(3) I read in Rails Guide that I should be doing this:
scope module: 'admin' do
resources :admin_permissions, :admin_layout, :admin_db
end
但我一直收到错误
ActionController::RoutingError at /admin_permissions/index
uninitialized constant AdminMainController
目录结构:
controllers
-> admin
-> admin_main_controller
-> admin_permissions_controller
-> admin_layouts_controller
-> admin_db_controller
views
-> admin
-> admin_main
-> admin_permissions
-> admin_layouts
-> admin_db
所有前三个控制器都继承自 admin_main,后者继承自应用程序
All first three controllers inherit from admin_main which inherits from application
关于阅读/检查什么的任何建议?
Any advise on what to read/check?
推荐答案
如果你执行 rake routes
,你会注意到所有控制器的名字都以 'admin/' 为前缀.
If you execute rake routes
, you will notice the names of all your controllers is prefixed with 'admin/'.
因为您将控制器移动到目录admin",您应该为那里的每个控制器添加一个命名空间.例如:
Because you move your controllers to directory "admin", you should add a namespace to each controller there. For example:
class Admin::AdminMainController < ActionController::Base
end
并检查您的控制器、视图和助手,更新所有相关路径和控制器名称.
And check your controllers, views and helpers, update all relevant paths and controller's names.
这篇关于Rails 4:在文件夹中对控制器进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!