Rails.env undefined in Rails 2

妖精的绣舞 提交于 2019-12-25 16:45:07

问题


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:

$ 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. But I seem to have a Rails object without an env method (even though I read that method was added in Rails 2). Any idea what's going on?


回答1:


The Rails module has always been in rails. It is not new in 3, so checking to see if it's defined is the wrong way to go about things. The correct way to determine if you can use Rails.env is to do Rails.respond_to? :env.




回答2:


Rails.env was introduced in Rails 2.3. You probably want to upgrade to Rails 2.3 if possible.

Otherwise, try this in a 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


来源:https://stackoverflow.com/questions/4411708/rails-env-undefined-in-rails-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!