Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 910
giorgio Avatar asked Apr 19 '12 05:04

giorgio


2 Answers

You can use like this

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

try it

like image 156
Kashiftufail Avatar answered Sep 16 '22 20:09

Kashiftufail


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

like image 34
tsherif Avatar answered Sep 17 '22 20:09

tsherif