简体   繁体   English

用Rails.root替换RAILS_ROOT

[英]Replacing RAILS_ROOT with Rails.root

I want to replace the deprecated RAILS_ROOT with Rails.root as part of the process of getting the application ready to upgrade to Rails 3. The application is currently running with version 2.3.14 and was originally created in 1.2 我想用Rails.root替换已弃用的RAILS_ROOT,作为使应用程序准备好升级到Rails 3的过程的一部分。该应用程序当前运行的版本为2.3.14,最初是在1.2中创建的

At the beginning of my boot.rb file is the line: 在我的boot.rb文件的开头是行:

RAILS_ROOT = "#{File.dirname( FILE )}/.." unless defined?(RAILS_ROOT) RAILS_ROOT =“#{File.dirname( FILE )} / ..”除非定义?(RAILS_ROOT)

I cant just substitute Rails.root here as Rails is undefined. 我不能在这里替换Rails.root,因为Rails是未定义的。

What is the recommended way of setting Rails.root? 设置Rails.root的推荐方法是什么?

I dont think I made the question clear enough. 我不认为我提出的问题很清楚。

  • I am aware that Rails.root already exists in Rails 2. 我知道Rails.root已经存在于Rails 2中。
  • I can use Rails.root in my code quite happily. 我可以非常愉快地在我的代码中使用Rails.root。
  • BUT I dont know how to SET Rails.root at startup. 但我不知道如何在启动时设置Rails.root。 I currently appears to be set right at the top of boot.rb with the line: 我目前似乎被设置在boot.rb顶部的行:

    RAILS_ROOT = "#{File.dirname( FILE )}/.." unless defined?(RAILS_ROOT) RAILS_ROOT =“#{File.dirname( FILE )} / ..”除非定义?(RAILS_ROOT)

  • I CANT just change that line to use Rails.root as I will get the error: 我只能更改该行以使用Rails.root,因为我将得到错误:

    ../config/boot.rb:3: uninitialized constant Rails (NameError) ../config/boot.rb:3:未初始化的常量Rails(NameError)

Thanks George 谢谢乔治

You can use like this 你可以像这样使用

 Rails.root.join('lib/ca-bundle.crt')

try it 试试吧

It looks like Rails.root actually existed in Rails 2.3: http://apidock.com/rails/v2.3.2/Rails/root/class 看起来Rails.root实际上存在于Rails 2.3中: httpRails.root

And in 2.3, it just returns RAILS_ROOT , so unless I'm mistaken, you can just use Rails.root in your code without other modifications. 在2.3中,它只返回RAILS_ROOT ,所以除非我弄错了,你可以在代码中使用Rails.root而无需其他修改。 When you do the upgrade, your boot.rb will be replaced, as will the Rails module, so you code should continue to work properly. 当您进行升级时,您的boot.rb将被替换, Rails模块也将被替换,因此您的代码应该继续正常运行。

Also, in case you're not using it already, my colleagues and I found this plugin extremely helpful when upgrading from 2.3 to 3.0 a little while back: https://github.com/rails/rails_upgrade 此外,如果你还没有使用它,我和我的同事发现这个插件非常有用,当时从2.3升级到3.0: https//github.com/rails/rails_upgrade

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM