I\'m running a Rails 5.2.0 application. This LoadError
always appears on the first request after a reboot or a recompile:
Unable to autoload consta
This same (LoadError) error occurred for my legacy Rails 5.1 application code, when I simply upgraded Rails to version 5.2.0. The fix (for me) was to add a missing source file that solely defines a submodule (to satisfy the autoloader). I will explain the fix in the context of the original post example.
In the original post, the ApplesController
is part of the V1
namespace. The problem is, that the V1
submodule is not (explicitly) defined. The solution is to create a file, at the proper (autoload) path, that defines the V1
module:
app/controllers/api/v1.rb
module Api
module V1
end
end
Apparently, as of Rails 5.2, the autoloader needs every module namespace to be explicitly defined. You can read the technical details about Rails 5.2 constant autoloading.
And a related SO answer to the same issue.