首页 > 解决方案 > 如何忽略 Zeitwerk for Rails 6 中的文件夹?

问题描述

简单的问题,但不知何故,答案让我无法理解。

在使用 Zeitwerk 迁移到 Rails 6 时,我得到:

Please, check the "Autoloading and Reloading Constants" guide for solutions.
 (called from <top (required)> at APP_ROOT/config/environment.rb:7)
rails aborted!
Zeitwerk::NameError: wrong constant name Enforce-calls-to-come-from-aws inferred by Module from directory

  APP_ROOT/app/junkyard/enforce-calls-to-come-from-aws

Possible ways to address this:

  * Tell Zeitwerk to ignore this particular directory.
  * Tell Zeitwerk to ignore one of its parent directories.
  * Rename the directory to comply with the naming conventions.

这看起来很棒:这是一个垃圾文件夹,永远不应该加载,所以忽略它是非常有意义的。

https://github.com/fxn/zeitwerk上的 Zeitwerk 文档说

tests = "#{__dir__}/**/*_test.rb"
loader.ignore(tests)
loader.setup

是您忽略文件夹的方式。很公平。但是我怎么找到loader?关于 Zeitwerk 自动加载的 Rails 指南(https://guides.rubyonrails.org/autoloading_and_reloading_constants.html)没有提到如何直接忽略文件夹,但确实提到了隐藏在的自动加载器Rails.autoloaders.main,所以我认为

Rails.autoloaders.main.ignore("#{__dir__}/junkyard/**/*.rb")

或者

Rails.autoloaders.main.ignore("#{__dir__}/app/junkyard/**/*.rb")

将是要走的路。没运气。我试过把它放进application.rb去,initializers/zeitwerk.rb但都没有奏效。

任何想法在哪里以及如何在 Rails 中使用 Zeitwerk 忽略文件夹?

PS:是的,我知道我应该从 中删除它app,我会的。但问题仍然令人头疼。

标签: ruby-on-railsrubyruby-on-rails-6

解决方案


我遇到了同样的问题,结果它抱怨文件夹名称。

将其添加到application.rb可能对您有用:

Rails.autoloaders.main.ignore(Rails.root.join('app/junkyard'))


推荐阅读