Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's different between RACK_ENV and RAILS_ENV?

I want to get the current enviroment of my rails application. But I don't konw what's different between RACK_ENV and RAILS_ENV? Who can help me?

Why sometimes RACK_ENV is empty, but RAILS_ENV has value?

like image 604
rainstop3 Avatar asked Aug 26 '14 04:08

rainstop3


3 Answers

You can use RACK_ENV as well as RAILS_ENV except for RAILS_ENV is prior to RACK_ENV.

rails/rails.rb at b0b4b176b0e061a4f03ddce669637b7d6c37aa33 · rails/rails

like image 54
Shinichi Maeshima Avatar answered Nov 13 '22 05:11

Shinichi Maeshima


Rails applications uses RAILS_ENV. Other Rack-based applications use RACK_ENV. If you have a Rails application, ignore RACK_ENV.

EDIT: The other answer is more correct.

like image 33
Amadan Avatar answered Nov 13 '22 05:11

Amadan


def set_environment
  ENV["RAILS_ENV"] ||= options[:environment]
end

ENV["RAILS_ENV"] is from options

def options
  merged_options = @use_default_options ? default_options.merge(@options) : @options
  merged_options.reject { |k, v| @ignore_options.include?(k) }
end

if default rails server, environment is from default options

def default_options
  environment  = ENV['RACK_ENV'] || 'development'
  default_host = environment == 'development' ? 'localhost' : '0.0.0.0'

  {
    :environment => environment,
    :pid         => nil,
    :Port        => 9292,
    :Host        => default_host,
    :AccessLog   => [],
    :config      => "config.ru"
  }
end

so, ENV['RACK_ENV'] has default value 'development'

like image 1
梁宇哲 Avatar answered Nov 13 '22 07:11

梁宇哲