开发者

Why is my Rails.root nil?

开发者 https://www.devze.com 2023-01-26 19:04 出处:网络
I\'m trying to开发者_JS百科 reference Rails.root in my application.rb but it is nil, why is that?I can explain why, but I can\'t give you a workaround.

I'm trying to开发者_JS百科 reference Rails.root in my application.rb but it is nil, why is that?


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__)


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


I had the same issue when I tried to use it before the module and class declaration. Try using it inside and see if that makes a difference e.g.

module MyApp
  class Application < Rails::Application

    puts Rails.root

  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消