Rails 6:Zeitwerk::NameError 不从模块加载类

Kam*_*Kam 15 ruby ruby-on-rails

我有一个看起来像这样的文件

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

    class Base < ::Account::Base
      def self.call(*attrs)
        raise NotImplementedError
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

现在当我运行代码时rails c出现错误

> ::Account::Authenticate::AuthenticateError
=> NameError (uninitialized constant Account::Authenticate::AuthenticateError)
> ::Account::Authenticate.constants
=> [:Base, :ViaToken]
Run Code Online (Sandbox Code Playgroud)

所以 rails 没有看到 AuthenticateError 类。但是当我从这个文件夹创建一个嵌套类时

=> Account::Authenticate::ViaToken
> ::Account::Authenticate.constants
=> [:Base, :AuthenticateError, :ViaToken]
Run Code Online (Sandbox Code Playgroud)

AuthenticateError 类现在可见

> ::Account::Authenticate::AuthenticateError
=> Account::Authenticate::AuthenticateError
Run Code Online (Sandbox Code Playgroud)

这个问题的解决方案是创建一个单独的文件authenticate_error.rb,它从一开始就可以工作,但这个解决方案对我来说并不理想。是否有任何解决方案可以预加载所有类或 smth?

(Ruby 2.6 和 Rails 6.0.0.rc2)

Pro*_*ton 23

在将 Rails 6.0.2 应用程序部署到 Ubuntu 18.04 服务器时,我遇到了同样的问题。

无法加载应用程序:Zeitwerk::NameError: 预期文件 /home/deploy/myapp/app/models/concerns/designation.rb 定义常量指定,但没有

我发现问题出在zeitwerk 上。Zeitwerk 是 Rails 6 中使用的新代码加载器引擎。它旨在成为所有 Rails 6+ 项目的新默认引擎,取代旧的经典引擎。Zeitwerk 提供代码自动加载、预先加载和重新加载的功能。

这是我解决它的方法

导航到config/application.rb项目中的文件。

在您的应用程序模块中添加此行以切换到classic自动加载模式:

config.autoloader = :classic
Run Code Online (Sandbox Code Playgroud)

下面是一个例子:

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
Run Code Online (Sandbox Code Playgroud)

您可以在这篇文章中阅读有关zeitwerk 的更多信息:Understanding Zeitwerk in Rails 6

就这样。

我希望这有帮助

  • 成功了。多谢! (2认同)

Jef*_*oli 7

将 Rails 应用程序从 5.2 更新到 6.0,并且还实现了 Zeitwerk 升级!

如果您想继续使用当前使用的自动加载模式,避免使用 Zeitwerk,则将此行添加到您的 application.rb 文件中(@PromisePreston 答案Rails 文档

config.autoloader = :classic
Run Code Online (Sandbox Code Playgroud)

如果您想升级到 Zeitwerk,则可以使用以下命令bin/rails zeitwerk:check(摘自本指南文章)。

我们最接近这个特定问题的场景是我们在子文件夹中有一个文件,如下所示:

#presenters/submission_files/base.rb
module Presenters
  module SubmissionFiles
    class Base < Showtime::Presenter
      def method_call
        #code_here
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

删除额外的模块以获得:

 #presenters/submission_files/base.rb
 module Presenters
   class SubmissionFiles::Base < Showtime::Presenter
      def method_call
        #code_here
      end
   end
 end
Run Code Online (Sandbox Code Playgroud)

然后,当调用应用程序中其他 ruby​​ 文件中的方法时,请使用:Presenters::SubmissionFiles::Base.method_call