Rails.env 在 Rails 2 中未定义
我有一个运行 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 时为 Rails.env,未定义时为 RAILS_ENV。但我似乎有一个没有 env
方法的 Rails 对象(尽管我读到该方法是在 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
:
$ 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails.env 是在 Rails 2.3 中引入的。如果可能的话,您可能希望升级到 Rails 2.3。
否则,请在 config/initializers/rails_env.rb 中尝试此操作:
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
:Rails
模块始终位于 Rails 中。它在 3 中并不是新的,因此检查它是否已定义是错误的处理方式。确定是否可以使用 Rails.env 的正确方法是执行 Rails.respond_to? :env。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 useRails.env
is to doRails.respond_to? :env
.