为什么我的 Rails.root 为零? [英] Why is my Rails.root nil?

查看:35
本文介绍了为什么我的 Rails.root 为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 application.rb 中引用 Rails.root 但它是零,这是为什么?

I'm trying to 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.rootrails/railties/中定义lib/rails.rb

def root
  application && application.config.root
end

application.rb中,还没有创建application的实例,因为Application类正在被定义... 应用程序仅在 environment.rb 之后初始化:

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!

编辑

解决方法就在我们眼前:

The workaround is right before our eyes:

my_rails_root = File.expand_path('../..', __FILE__)

这篇关于为什么我的 Rails.root 为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆