【问题标题】:Autoloading and Zeitwerk Mode in Rails 6Rails 6 中的自动加载和 Zeitwerk 模式
【发布时间】:2019-12-27 19:27:34
【问题描述】:

在 Rails 6 的 Zeitwerk 模式中是否有任何代码被贬值?

 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")

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-6


    【解决方案1】:

    您在此处的所有内容均已弃用,但值得一提的是来自documentation

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

    Rails 5+ 不鼓励使用手动扩展 config.autoload_paths,因为它可能在您的生产环境中引起潜在问题。讨论可以追溯到 2013 年,您可以阅读它here

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 2021-05-31
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多