Search code examples
ruby-on-railsruby-on-rails-4activesupport

LoadError: Unable to autoload constant ApplicationController


I'm somewhat confused by the following 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

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 (e.g. 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. 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.

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.

How can I avoid this LoadError?


Solution

  • 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.

    Everything inside app/ is already unloadable by default and doesn't require the explicit call.

    For more information, look at this issue in the Rails repository on Github.