我正在尝试将我的一个应用程序整合到控制器和视图的子文件夹中—一个用于营销网站,另一个用于应用程序本身。
这就是我现在拥有的:
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设计不知道应用程序控制器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!'
最佳答案
正如@benchwarmer所说,如果您确实在子目录中放置了一个控制器,那么您需要相应地命名类名;在您的情况下,App::ApplicationController
。然而,看起来您需要使所有现有的控制器从App::ApplicationController
继承,而不是从ApplicationController
继承。为什么不将ApplicationController
保持在顶层,如果您需要主应用程序或营销应用程序的其他方法,请在Marketing::MarketingController
中创建一个marketing/
来扩展ApplicationController
,然后营销中的所有控制器都可以扩展,同样地,也可以为应用程序目录创建。或者,您可以将营销控制器放在marketing子目录和marketing::命名空间中,并将应用控制器放在controllers/
中,而不是为它们单独设置一个子目录。不管怎样,由你决定。另一个难题是,如果您移动applicationcontroller(或希望designe从任意控制器继承),则需要向初始化程序添加:
config.parent_controller = "App::ApplicationController"
在您的例子中,如果您将应用程序控制器移动到应用程序/子目录中并命名它。这告诉develve它的控制器应该从哪个控制器继承,它默认为
devise.rb
,这就是为什么当您移动它时它找不到它。关于ruby-on-rails - 在子文件夹中设计带有 Controller 的UninitializedConstant ApplicationController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21743964/