设计未初始化常量应用程序控制器,控制器位于子文件夹中



我正在尝试将我的一个应用程序合并到控制器和视图到子文件夹中 - 一个用于营销网站,一个用于应用程序本身。

这是我目前拥有的:

app/controllers
application_controller.rb
...shit ton of other controllers...

这就是我要做的:

app/controllers/app
application_controller.rb
...all controllers related to the app itself...
app/controllers/marketing
...all controllers related to the marketing site...

营销网站运行良好,因为不需要身份验证,但该应用程序正在轰炸b/c Designs不知道application_controller.rb现在处于app/controllers/app/application_controller.rb

如何告知设备控制器的位置?

这是我设计的路线:

devise_for :users, :skip => [:sessions]
as :user do
get 'login' => 'devise/sessions#new', :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
get 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
end

堆栈跟踪的一部分:

NameError - uninitialized constant ApplicationController:
activesupport (3.2.12) lib/active_support/dependencies.rb:520:in `load_missing_constant'
activesupport (3.2.12) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.12) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.12) lib/active_support/inflector/methods.rb:230:in `block in constantize'
activesupport (3.2.12) lib/active_support/inflector/methods.rb:229:in `constantize'
activesupport (3.2.12) lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
devise (2.2.4) app/controllers/devise_controller.rb:2:in `<top (required)>'
activesupport (3.2.12) lib/active_support/dependencies.rb:469:in `block in load_file'
activesupport (3.2.12) lib/active_support/dependencies.rb:639:in `new_constants_in'
activesupport (3.2.12) lib/active_support/dependencies.rb:468:in `load_file'
activesupport (3.2.12) lib/active_support/dependencies.rb:353:in `require_or_load'
activesupport (3.2.12) lib/active_support/dependencies.rb:502:in `load_missing_constant'
activesupport (3.2.12) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.12) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.12) lib/active_support/dependencies.rb:514:in `load_missing_constant'
activesupport (3.2.12) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.12) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.12) lib/active_support/dependencies.rb:514:in `load_missing_constant'
activesupport (3.2.12) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.12) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.12) lib/active_support/dependencies.rb:514:in `load_missing_constant'
activesupport (3.2.12) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.12) lib/active_support/dependencies.rb:190:in `const_missing'
devise (2.2.4) lib/devise/controllers/helpers.rb:80:in `devise_controller?'
devise (2.2.4) lib/devise/controllers/helpers.rb:48:in `authenticate_user!'

正如@benchwarmer所说,如果您确实将控制器放在子目录中,则需要相应地为类名命名;App::ApplicationController在你的情况下。 但是,您似乎需要使所有现有控制器继承自App::ApplicationController而不是ApplicationController。 为什么不将ApplicationController保留在顶层,如果您需要主应用或营销应用的其他方法,例如,在marketing/中创建一个扩展ApplicationControllerMarketing::MarketingController,然后营销中的所有控制器都可以扩展,同样适用于应用目录。 或者,可以将营销控制器放在市场营销子目录和 Marketing:: 命名空间中,并将应用控制器保留在controllers/中,而不是为它们设置单独的子目录。 无论如何,由你决定。 难题的另一部分是,如果您移动 ApplicationController(或希望 Design 从任何任意控制器继承),则需要添加到您的devise.rb初始化器中:

config.parent_controller = "App::ApplicationController"

在您的情况下,如果您将应用程序控制器移动到 app/子目录中并为其命名空间。 这告诉设计它的控制器应该从哪个控制器继承,它默认为ApplicationController这就是为什么当你移动它时找不到它的原因。

不应将application_controller.rb文件从其原始位置移动,而应将其保留在其原始位置。就文件的命名类而言app/controller/app/application_controller.rb它应该是

class App::ApplicationController < ApplicationController
...
end

和此目录下的其他控制器继承自App::ApplicationController

最新更新