简体   繁体   English

LoadError:无法自动加载常量ApplicationController

[英]LoadError: Unable to autoload constant ApplicationController

I'm somewhat confused by the following LoadError. 我对以下LoadError感到困惑。

LoadError - Unable to autoload constant ApplicationController, expected /opt/rails/production/current/app/controllers/application_controller.rb to define it:
  activesupport (4.2.4) lib/active_support/dependencies.rb:495:in `load_missing_constant'
  activesupport (4.2.4) lib/active_support/dependencies.rb:184:in `const_missing'
  app/controllers/display_controller.rb:3:in `<top (required)>'
  activesupport (4.2.4) lib/active_support/dependencies.rb:457:in `block in load_file'
  activesupport (4.2.4) lib/active_support/dependencies.rb:647:in `new_constants_in'
  activesupport (4.2.4) lib/active_support/dependencies.rb:456:in `load_file'
  activesupport (4.2.4) lib/active_support/dependencies.rb:354:in `require_or_load'
  activesupport (4.2.4) lib/active_support/dependencies.rb:494:in `load_missing_constant'
  activesupport (4.2.4) lib/active_support/dependencies.rb:184:in `const_missing'
  activesupport (4.2.4) lib/active_support/inflector/methods.rb:261:in `block in constantize'
  activesupport (4.2.4) lib/active_support/inflector/methods.rb:259:in `constantize'
  activesupport (4.2.4) lib/active_support/dependencies.rb:566:in `get'
  activesupport (4.2.4) lib/active_support/dependencies.rb:597:in `constantize'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:72:in `controller_reference'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:62:in `controller'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:41:in `serve'
  actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
  meta_request (0.3.4) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
  meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
  bullet (4.14.7) lib/bullet/rack.rb:12:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.4) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
  rack-contrib (1.4.0) lib/rack/contrib/response_headers.rb:17:in `call'
  meta_request (0.3.4) lib/meta_request/middlewares/headers.rb:16:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.4) lib/rails/rack/logger.rb:22:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.4) lib/rails/engine.rb:518:in `call'
  railties (4.2.4) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /home/production/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  /home/production/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  /home/production/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

I've limited my controller to just a method and it still does this, so its not related to the code in the controller. 我已经将控制器限制为仅一种方法,并且它仍然会执行此操作,因此它与控制器中的代码无关。

Here's the routes for it: 这是路线:

namespace :display do
    get :delivery
end

Here are the development options: 以下是开发选项:

  config.cache_classes = false

  config.eager_load = false

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  config.assets.js_compressor = :uglifier

  config.serve_static_files = true

  config.action_dispatch.cookies_serializer = :hybrid

  config.active_record.raise_in_transactional_callbacks = true

Here is my ApplicationController 这是我的ApplicationController

class ApplicationController < ActionController::Base
  include AuthenticatedSystem

  helper :all

  unloadable
  protect_from_forgery

  rescue_from ActionController::InvalidAuthenticityToken, :with => :bad_token

  def fail
    raise "Well, that was exciting."
  end

  private

  def bad_token
    flash[:notice] = "Your session has expired."
    redirect_to :controller => :account, :action => :login
  end

  def authorize_edit
    authorized! "Edit " + @project.data_type
  end

  def authorize_view
    authorized! "View " + @project.data_type
  end

  def set_project
    @project = Project.find_by(id: params[:project_id])
  end

end

This occurs with various controllers, and consistently always right after I make a change in code. 这在各种控制器中都会发生,并且始终在我更改代码后立即发生。 It doesn't seem to matter where the change occurs (eg controllers or models). 更改发生的位置(例如控制器或模型)似乎并不重要。 My only remedy is to restart the server every time I make a code change – very frustrating. 我唯一的补救方法是每次更改代码时都重新启动服务器–非常令人沮丧。

Other SO posts related to this recommend pluralizing the controller, but here a plural controller does not make sense, besides which, I've tried that and it doesn't work. 其他与此相关的SO帖子建议将控制器复数,但是在这里复数控制器没有任何意义,此外,我已经尝试过并且不起作用。 Besides, the auto loader just requires the filename to match the module/class name right? 此外,自动加载器只需要文件名来匹配模块/类名,对吗?

My problem is I'm just not sure what to fix based on the LoadError message in the stack trace. 我的问题是我不确定根据堆栈跟踪中的LoadError消息要解决的问题。

This seems related to how active support handles auto loading, I'm tempted to upgrade from Rails 4.2.4, to 4.2.5, but I'm not convinced that'll resolve this. 这似乎与主动支持如何处理自动加载有关,我很想从Rails 4.2.4升级到4.2.5,但是我不相信这会解决这个问题。

How can I avoid this LoadError? 如何避免此LoadError?

The use of unloadable in ApplicationController causes the controller class to be unloaded, and apparently not properly auto loaded again after something has changed in the development environment. ApplicationController使用unloadable会导致控制器类被卸载,并且在开发环境中发生某些更改之后,显然无法再次正确地自动加载。

Everything inside app/ is already unloadable by default and doesn't require the explicit call. 默认情况下, app/内容都已可以卸载,不需要显式调用。

For more information, look at this issue in the Rails repository on Github . 有关更多信息,请在Github上的Rails存储库中查看此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM