LoadError:无法自动加载常量ApplicationController

时间:2016-01-08 15:46:33

标签: ruby-on-rails ruby-on-rails-4 activesupport

我对以下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'

我将我的控制器限制为一个方法,它仍然这样做,因此它与控制器中的代码无关。

以下是它的路线:

namespace :display do
    get :delivery
end

以下是开发选项:

  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

这是我的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

在各种控制器中发生这种情况,并且在我对代码进行更改后,始终。发生变化的地方似乎并不重要(例如控制器或模型)。我唯一的补救办法是每次更改代码时重启服务器 - 非常令人沮丧。

与此相关的其他SO帖子建议多元化控制器,但这里有一个多个控制器没有意义,除此之外,我已经尝试过它并不起作用。此外,自动加载器只需要文件名匹配模块/类名吗?

我的问题是我只是不确定基于堆栈跟踪中的LoadError消息要修复什么。

这似乎与主动支持如何处理自动加载有关,我很想从Rails 4.2.4升级到4.2.5,但我不相信会解决这个问题。

如何避免此LoadError?

1 个答案:

答案 0 :(得分:1)

unloadable中使用ApplicationController会导致控制器类被卸载,并且在开发环境中发生某些变化后,显然没有再次正确地自动加载。

默认情况下,app/内的所有内容都已无法加载,并且不需要显式调用。

有关详细信息,请查看this issue in the Rails repository on Github