Iai*_*son 11 ruby ruby-on-rails ruby-on-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.
Run Code Online (Sandbox Code Playgroud)
这看起来很棒:这是一个垃圾文件夹,永远不应加载,因此忽略它是完全有道理的。
https://github.com/fxn/zeitwerk 上的 Zeitwerk 文档说
tests = "#{__dir__}/**/*_test.rb"
loader.ignore(tests)
loader.setup
Run Code Online (Sandbox Code Playgroud)
是您忽略文件夹的方式。很公平。但我如何找到loader?Zeitwerk 自动加载的 Rails 指南(https://guides.rubyonrails.org/autoloading_and_reloading_constants.html)没有提到如何直接忽略文件夹,但确实提到了隐藏在 的自动加载器Rails.autoloaders.main,所以我认为
Rails.autoloaders.main.ignore("#{__dir__}/junkyard/**/*.rb")
Run Code Online (Sandbox Code Playgroud)
或者
Rails.autoloaders.main.ignore("#{__dir__}/app/junkyard/**/*.rb")
Run Code Online (Sandbox Code Playgroud)
将是要走的路。没运气。我试过把它放进application.rb去initializers/zeitwerk.rb,但都没有奏效。
任何想法在哪里以及如何在 Rails 中使用 Zeitwerk 忽略文件夹?
PS:是的,我知道我应该从 中删除它app,我会的。但这个问题仍然令人烦恼。
小智 33
我遇到了同样的问题,结果发现它在抱怨文件夹名称。
将此添加到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'))
...
Run Code Online (Sandbox Code Playgroud)