简体   繁体   English

Rails 2中未定义的Rails.env

[英]Rails.env undefined in Rails 2

I have an older application running Ruby on Rails 2.2, and I'm having trouble getting a gem to work because it can't find the current environment with Rails.env : 我有一个运行Ruby on Rails 2.2的较旧的应用程序,但是我无法使gem正常工作,因为它无法通过Rails.env找到当前环境:

$ script/console
>> Rails.env
NoMethodError: undefined method `env' for Rails:Module
from (irb):1

The gem is set up to use Rails.env when Rails is defined, and RAILS_ENV when it's not. 当定义Rails时,将gem设置为使用Rails.env ,将其设置为RAILS_ENV But I seem to have a Rails object without an env method (even though I read that method was added in Rails 2). 但是我似乎有一个没有env方法的Rails对象(即使我阅读了Rails 2中添加的方法)。 Any idea what's going on? 知道发生了什么吗?

Rails.env was introduced in Rails 2.3. Rails 2.3中引入了Rails.env You probably want to upgrade to Rails 2.3 if possible. 如果可能,您可能想升级到Rails 2.3。

Otherwise, try this in a config/initializers/rails_env.rb : 否则,请在config/initializers/rails_env.rb

require 'active_support/string_inquirer'
def Rails.env
  @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
end

The Rails module has always been in rails. Rails模块始终处于导轨中。 It is not new in 3, so checking to see if it's defined is the wrong way to go about things. 它不是3中的新功能,因此检查是否已定义是处理问题的错误方法。 The correct way to determine if you can use Rails.env is to do Rails.respond_to? :env 确定是否可以使用Rails.env的正确方法是执行Rails.respond_to? :env Rails.respond_to? :env . Rails.respond_to? :env

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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