“Unable to autoload constant” Bug in Rails 5.2.0

后端 未结 1 794
轻奢々
轻奢々 2020-12-11 23:19

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

相关标签:
1条回答
  • 2020-12-11 23:40

    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.

    0 讨论(0)
提交回复
热议问题