Rails 6: Zeitwerk::NameError doesn't load class from module

后端 未结 1 1156
渐次进展
渐次进展 2021-01-19 19:25

I have a file which looks like this

#app/services/account/authenticate/base.rb
module Account
  module Authenticate
    AuthenticateError = Class.new(Standar         


        
相关标签:
1条回答
  • 2021-01-19 20:04

    I experienced this same issue when deploying a Rails 6.0.2 application to an Ubuntu 18.04 server.

    Unable to load application: Zeitwerk::NameError: expected file /home/deploy/myapp/app/models/concerns/designation.rb to define constant Designation, but didn't

    I found out that the issue was with zeitwerk. Zeitwerk is the new code loader engine used in Rails 6. It’s meant to be the new default for all Rails 6+ projects replacing the old classic engine. Zeitwerk provides the features of code autoloading, eager loading, and reloading.

    Here's how I solved it:

    Navigate to the config/application.rb file on your project.

    Add this line within your application module to switch to classic mode for autoloading:

    config.autoloader = :classic
    

    Here's an example:

    module MyApp
      class Application < Rails::Application
        # Initialize configuration defaults for originally generated Rails version.
        config.load_defaults 6.0
    
        # Settings in config/environments/* take precedence over those specified here.
        # Application configuration can go into files in config/initializers
        # -- all .rb files in that directory are automatically loaded after loading
        # the framework and any gems in your application.
    
        config.autoloader = :classic
      end
    end
    

    You can read up more on zeitwerk in this article: Understanding Zeitwerk in Rails 6

    That's all.

    I hope this helps

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