ruby-on-rails - 如何忽略 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
,我会的。但问题仍然令人头疼。
解决方案
我遇到了同样的问题,结果它抱怨文件夹名称。
将其添加到application.rb
可能对您有用:
Rails.autoloaders.main.ignore(Rails.root.join('app/junkyard'))
推荐阅读
- java - java.io.FileNotFoundException:文件:/storage/emulated/0/WhatsApp/Media/.Statuses/xyz.jpg:打开失败:ENOENT(没有这样的文件或目录)
- java - 我对 Java 的方法有疑问,可以给我一些建议吗?
- webstorm - Nx.dev eslintrc.json
- windows - 如何修复 Lua 中的“无法初始化设备 PRN”?(使用 Vs 代码时)
- javascript - 你如何做动态网址。example.com/profile/CroatiaGM
- source-maps - Kotlin JS 源码图
- scripting - 不使用用户界面初始化/构建 Autodesk Maya 菜单
- c# - WebRequest.GetResponseAsync 引发无法捕获的异常
- django - DocuSign Connect - 错误 - 远程服务器返回错误:(503)服务器不可用
- amazon-web-services - 查看 VPC 中/链接到 VPC 的所有 AWS 资源