gpt4 book ai didi

ruby-on-rails - 使用子文件夹中的 Controller 设计 UninitializedConstant ApplicationController

转载 作者:数据小太阳 更新时间:2023-10-29 07:50:00 24 4
gpt4 key购买 nike

我正在尝试将我的一个应用程序合并到 Controller 中,并将 View 合并到子文件夹中 - 一个用于营销网站,一个用于应用程序本身。

这是我目前拥有的:

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 devise 不知道 application_controller.rb 现在在 app/controllers/app/application_controller.rb

我如何告诉设计我的 Controller 的位置?

这是我的设计路线:

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 所说,如果您确实将 Controller 放在子目录中,则需要相应地命名类名; App::ApplicationController 在你的情况下。但是,您似乎需要让所有现有 Controller 继承自 App::ApplicationController 而不是 ApplicationController。为什么不将 ApplicationController 保持在顶层,如果您需要主应用程序或营销应用程序的其他方法,请在 中创建一个 Marketing::MarketingController >marketing/ 扩展了 ApplicationController 并且 marketing 中的所有 Controller 都可以扩展,对于 app 目录也是如此。或者,您可以将营销 Controller 放在营销子目录和 Marketing::命名空间中,并将您的应用程序 Controller 留在 controllers/ 中,而不是为它们创建单独的子目录。无论如何,由你决定。另一个难题是,如果您移动 ApplicationController(或希望设计从任意 Controller 继承),您将需要添加到您的 devise.rb 初始化程序:

config.parent_controller = "App::ApplicationController"

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

关于ruby-on-rails - 使用子文件夹中的 Controller 设计 UninitializedConstant ApplicationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21743964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com