Rails 6 中的自动加载和 Zeitwerk 模式 [英] Autoloading and Zeitwerk Mode in Rails 6

查看:38
本文介绍了Rails 6 中的自动加载和 Zeitwerk 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些代码是否在 Rails 6 的 Zeitwerk 模式中折旧?

Is any of this code depreciated in Zeitwerk Mode in Rails 6?

 class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2

    # config/application.rb
    config.i18n.load_path += Dir[Rails.root.join("config", "locales", "**", "*.{rb,yml}")]
    config.i18n.fallbacks = true

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
    config.autoload_paths += ["#{config.root}/app/queries/"]


    # https://gist.github.com/maxim/6503591 (should remove this and fix)
    config.eager_load_paths << Rails.root.join("lib")

我读到自动加载正在被删除,到目前为止它没有引起问题,但想删除不推荐使用的代码.如果折旧了,我该如何加载我的代码?

I read that autoloading is being removed, and so far it has not caused an issue but wanted to remove deprecated code. If it is depreciated, how do I load my code?

推荐答案

这里没有不推荐使用的内容,但值得一提的是 文档:

Nothing you have here is deprecated, however one thing worth mentioning is from the documentation:

自动加载路径数组可以通过改变 config/application.rb 中的 config.autoload_paths 来扩展,但现在不鼓励这样做.

The array of autoload paths can be extended by mutating config.autoload_paths, in config/application.rb, but nowadays this is discouraged.

Rails 5+ 不鼓励使用手动扩展 config.autoload_paths,因为它可能会在您的生产环境中导致潜在问题.讨论可以追溯到 2013 年,您可以在此处阅读相关内容.

Rails 5+ is discouraging the use of manually extending config.autoload_paths because of potential issues it can cause in your production environment. The discussion dates back to 2013 and you can read about it here.

从 Rails 5+ 开始,app/ 下的所有目录默认自动加载.如果你想遵循 Rails 的建议,你应该删除这一行

From Rails 5+ all directories under app/ are autoloaded by default. If you'd like to follow the Rails recommendation, you should remove this line

config.autoload_paths += ["#{config.root}/app/queries/"]

并将您的查询目录移动到 "#{Rails.root}/app" 文件夹下.

and move your queries directory under "#{Rails.root}/app" folder.

这篇关于Rails 6 中的自动加载和 Zeitwerk 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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