Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Rails.root nil?

I'm trying to reference Rails.root in my application.rb but it is nil, why is that?

like image 638
Blankman Avatar asked Nov 21 '10 21:11

Blankman


2 Answers

I can explain why, but I can't give you a workaround.

Rails.root is defined in rails/railties/lib/rails.rb

def root
  application && application.config.root
end

In application.rb, the instance of application is not yet created, because the Application class is being defined... The application is only initialized after, in environment.rb:

# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
Testapp::Application.initialize!

EDIT

The workaround is right before our eyes:

my_rails_root = File.expand_path('../..', __FILE__)
like image 82
Thomas Guillory Avatar answered Nov 17 '22 04:11

Thomas Guillory


Are you using Rails 3.x? If not, you should be using RAILS_ROOT rather than Rails.root.

like image 1
Lucas Willett Avatar answered Nov 17 '22 04:11

Lucas Willett