简单的问题,但不知为何我却找不到答案。
在使用 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.
这看起来很棒:这是一个垃圾文件夹,永远不应该加载,所以忽略它是完全有道理的。
Zeitwerk 文档位于 https://github.com/fxn/zeitwerk say
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
中删除,我会的。但这个问题仍然令人烦恼。
我遇到了同样的问题,结果发现它在抱怨文件夹名称。
将此添加到
application.rb
可能对您有用:
Rails.autoloaders.main.ignore(Rails.root.join('app/junkyard'))
我将此添加到
config/initializers/zeitwerk.rb
:
Rails.autoloaders.each do |autoloader|
autoloader.ignore(Rails.root.join('app/ui'))
...