rainstop3
rainstop3

Reputation: 1448

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?

Upvotes: 22

Views: 5905

Answers (3)

梁宇哲
梁宇哲

Reputation: 11

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'

Upvotes: 1

Amadan
Amadan

Reputation: 198324

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.

Upvotes: 10

Shinichi Maeshima
Shinichi Maeshima

Reputation: 461

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

Upvotes: 33

Related Questions