如何忽略 Zeitwerk for Rails 6 中的文件夹? [英] How to ignore a folder in Zeitwerk for Rails 6?

查看:32
本文介绍了如何忽略 Zeitwerk for Rails 6 中的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,但不知何故我逃过了答案.

Simple question, but somehow the answer escapes me.

随着 Zeitwerk 迁移到 Rails 6,我得到:

In moving to Rails 6 with Zeitwerk, I get:

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.

这看起来很棒:这是一个垃圾文件夹,永远不应加载,因此忽略它是完全有道理的.

Which seems great: that's a junk folder and should never be loaded, so ignoring it makes perfect sense.

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

The Zeitwerk docs at 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 中的自动加载器,所以我想

is how you ignore a folder. Fair enough. But how do I find loader? The Rails guide on Zeitwerk autoloading (https://guides.rubyonrails.org/autoloading_and_reloading_constants.html) doesn't mention how to ignore folders directly, but does mention the autoloader stashed at Rails.autoloaders.main, so I figured that

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

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

将是要走的路.没有运气.我试过把它放在 application.rbinitializers/zeitwerk.rb 中,但都没有奏效.

would be the way to go. No luck. I've tried putting this in application.rb and in initializers/zeitwerk.rb and neither worked.

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

Any ideas where and how to ignore a folder with Zeitwerk within Rails?

PS:是的,我知道我应该从 app 中删除它,我会的.但这个问题仍然令人烦恼.

PS: yes, I know I should just remove this from app, and I will. But the question still vexes.

推荐答案

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

I ran into this same issue, and it turns out it was complaining about a folder name.

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

Adding this to application.rb may work for you:

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

这篇关于如何忽略 Zeitwerk for Rails 6 中的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆