使用子文件夹中的控制器设计UninitializedConstant ApplicationController

时间:2014-02-13 02:23:53

标签: ruby-on-rails ruby ruby-on-rails-3 devise

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

这就是我目前所拥有的:

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设计并不知道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

stacktrace的一部分:

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!'

2 个答案:

答案 0 :(得分:4)

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

config.parent_controller = "App::ApplicationController"

在您的情况下,如果您将应用程序控制器移动到app /子目录并命名它。这告诉devise它的控制器应该从哪个控制器继承,它默认为ApplicationController,这就是为什么它在移动它时无法找到它。

答案 1 :(得分:2)

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

class App::ApplicationController < ApplicationController
  ...
end

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