我在我的config/environment.rb文件中设置了ENV["RAILS_ENV"] = "production",以便在我的机器上运行我的服务器(使用rails server)并获得生产行为。我的代码中有很多行用来检查Rails.env.production?是否会为一些应用程序组件分配不同的功能。我的问题是,当我在我的一个控制器中检查环境时,我得到了Rails.env和ENV["RAILS_ENV"]不同的结果。第一个将显示“开发”,而第二个将显示“生产”。
这两个方法不应该返回相同的值吗?
发布于 2015-06-16 20:36:08
在计算config/environment.rb ment.rb时,您只是修改了ENV散列。如果希望在生产环境中运行应用程序,请在用于运行rails的shell中设置RAILS_ENV环境变量。
RAILS_ENV=production套件exec rails c
发布于 2015-06-16 20:38:13
要在生产模式下运行rails服务器,请运行:
rails s -e production回答你的实际问题:
Rails.env在内部使用ENV["RAILS_ENV"],请参阅:https://github.com/rails/rails/blob/d25fe31c40928712b5e08fe0afb567c3bc88eddf/railties/lib/rails.rb#L59-L61
def env
@_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
end但是直到现在还没有设置ENV["RAILS_ENV"],所以在触发rails服务器命令时传递给-e的选项就会出现在画面中,请参见:
def set_environment
ENV["RAILS_ENV"] ||= options[:environment]
end opts.on("-e", "--environment=name", String,
"Specifies the environment to run this server under (test/development/production).",
"Default: development") { |v| options[:environment] = v }所有这些都是在你的应用程序environment.rb执行之前发生的。
希望这能有所帮助。
https://stackoverflow.com/questions/30867722
复制相似问题