Rails 4: Grouping Controllers in a folder

﹥>﹥吖頭↗ 提交于 2019-12-04 19:29:48

问题


I would like to group all my controllers and their views in folders for example under "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) I read in Rails Guide that I should be doing this:

scope module: 'admin' do
  resources :admin_permissions, :admin_layout, :admin_db
end

but I keep getting an error

ActionController::RoutingError at /admin_permissions/index
uninitialized constant AdminMainController

Directory Structure:

controllers
  -> admin
    -> admin_main_controller
    -> admin_permissions_controller
    -> admin_layouts_controller
    -> admin_db_controller
views
  -> admin
    -> admin_main
    -> admin_permissions
    -> admin_layouts
    -> admin_db

All first three controllers inherit from admin_main which inherits from application

Any advise on what to read/check?


回答1:


If you execute rake routes, you will notice the names of all your controllers is prefixed with '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.



来源:https://stackoverflow.com/questions/18264446/rails-4-grouping-controllers-in-a-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!