简体   繁体   English

升级 Rails,如何在 config/environment/devlopment.rb 运行之前自动加载常量?

[英]Upgrading Rails, how to autoload constants before config/environment/devlopment.rb runs?

I'm new to Ruby and Rails, so please forgive me if this is a n00b question.我是 Ruby 和 Rails 的新手,所以如果这是一个 n00b 问题,请原谅我。 I'm trying to upgrade a very old app (using Ruby 2.3.4 and Rails 5.0.6) to the latest versions of Ruby (3.1.3) and Rails (7.0.4).我正在尝试将一个非常旧的应用程序(使用 Ruby 2.3.4 和 Rails 5.0.6)升级到最新版本的 Ruby (3.1.3) 和 Rails (7.0.4)。 When config/environment/devlopment.rb executes, I get an uninitialized constant error.当 config/environment/devlopment.rb 执行时,我得到一个未初始化的常量错误。 It's trying to access a constant that's defined in a file /lib/settings.rb.它试图访问文件 /lib/settings.rb 中定义的常量。 If I try to use this constant in a controller, I have no problems.如果我尝试在 controller 中使用这个常量,我没有问题。 However, trying to use this constant in my development.rb causes the error.但是,尝试在我的 development.rb 中使用此常量会导致错误。

I've already added these lines to my application.rb, but it hasn't helped:我已经将这些行添加到我的 application.rb,但没有帮助:

config.autoload_paths << "#{Rails.root}/lib"
config.eager_load_paths << "#{Rails.root}/lib"

From my research, this seems to be a problem with autoloading, and that the new Rails uses something called Zeitwerk which does loading a bit differently.根据我的研究,这似乎是自动加载的问题,而且新的 Rails 使用了一种叫做 Zeitwerk 的东西,它的加载方式有点不同。 However, I'm not sure how to make this work.但是,我不确定如何进行这项工作。 Is there a way to get this constant to load before development.rb executes?有没有办法让这个常量在 development.rb 执行之前加载?

Files in lib are not supposed to be autoloaded. lib中的文件不应自动加载。 Please don't add those configuration lines, instead, issue a请不要添加这些配置行,而是发出一个

require 'settings'

in config/environments/development.rb .config/environments/development.rb中。

The reason for the error is likely caused by changes in how Rails handles autoloading in the latest versions.错误的原因可能是 Rails 在最新版本中处理自动加载的方式发生了变化。 In previous versions of Rails, constants defined in lib were automatically loaded, but in newer versions this is not the case.在以前的 Rails 版本中,lib 中定义的常量会自动加载,但在较新的版本中,情况并非如此。

One solution is to use the require_dependency method in your development.rb file to explicitly require the file that defines the constant.一种解决方案是在 development.rb 文件中使用 require_dependency 方法来明确要求定义常量的文件。 For example:例如:

require_dependency "#{Rails.root}/lib/settings"

Alternatively, you can use the config.autoload_paths and config.eager_load_paths settings in your application.rb file to specify that Rails should automatically load files in the lib directory.或者,您可以在 application.rb 文件中使用 config.autoload_paths 和 config.eager_load_paths 设置来指定 Rails 应自动加载 lib 目录中的文件。 However, based on your existing configuration this should be already covered.但是,根据您现有的配置,这应该已经涵盖。

Another option is to move the constant definition to a file located under the app directory, which will be automatically loaded by Rails.另一种选择是将常量定义移动到位于 app 目录下的文件中,该文件将由 Rails 自动加载。

It's also worth noting that upgrading such a large version gap of Ruby and Rails may require significant effort and testing, and it's recommended to take a backup of the current application before proceeding with the upgrade.另外值得注意的是,升级 Ruby 和 Rails 如此大的版本差距可能需要大量的努力和测试,建议在继续升级之前备份当前应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Rails项目中,我们将所有常量定义放在config / environment.rb中,将更多特定定义放在config / environments / development.rb中? - In a Rails project, we put all constants definition in config/environment.rb and more particular ones in config/environments/development.rb? 在environment.rb中使用全局常量的Rails - Rails using global constants in environment.rb 我应该将Rails项目的常量放在environment.rb中吗? - Should I put constants for my Rails project in environment.rb? 配置下的environment.rb文件出错(Ruby on Rails) - Error on environment.rb file under config (Ruby on rails) 如何将常量延迟加载到Rails环境中 - How to lazy load constants into rails environment 在Rails中的config / initializers / constants.rb中更改时,全局值不更新 - global values not updating when changed in config/initializers/constants.rb in Rails 为什么Rails应用程序会在environment.rb中显式声明config.gem&#39;rail&#39;? - Why would a Rails application explicitly declare config.gem 'rails' in environment.rb? Rails 3 Deploy:多个Rails应用程序单个域错误:无此类文件或目录-config / environment.rb - Rails 3 Deploy: Multiple rails apps single domain error: No such file or directory - config/environment.rb 如何在Rails中定义第二个environment.rb文件? - How do i define a second environment.rb file in rails? 升级Rails环境 - Upgrading Rails Environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM