简体   繁体   中英

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

At the beginning of my boot.rb file is the line:

RAILS_ROOT = "#{File.dirname( FILE )}/.." unless defined?(RAILS_ROOT)

I cant just substitute Rails.root here as Rails is undefined.

What is the recommended way of setting Rails.root?

I dont think I made the question clear enough.

  • I am aware that Rails.root already exists in Rails 2.
  • I can use Rails.root in my code quite happily.
  • BUT I dont know how to SET Rails.root at startup. I currently appears to be set right at the top of boot.rb with the line:

    RAILS_ROOT = "#{File.dirname( FILE )}/.." unless defined?(RAILS_ROOT)

  • I CANT just change that line to use Rails.root as I will get the error:

    ../config/boot.rb:3: uninitialized constant 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

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. When you do the upgrade, your boot.rb will be replaced, as will the Rails module, so you code should continue to work properly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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