i.e. when I'm running the app in test mode (using rails server) or maybe some other configuration it's running in develop mode (with no asset compilation, or caching, etc) but when I deploy it to a server its running in production mode.
How does the app determine what environment configuration to use?
When we generate a new Rails application we get three environments by default: development , test and production . There's nothing special about these particular environments, though, and there are very few places in the Rails source code that refer to them.
Rails comes packages with 3 types of environments. Each have its own server, database, and configuration. See Rails Guides: Configuration for more information on options available to you.
Gmail Example Instead use the Ruby variable ENV["GMAIL_USERNAME"] to obtain an environment variable. The variable can be used anywhere in a Rails application. Ruby will replace ENV["GMAIL_USERNAME"] with an environment variable. Let's consider how to set local environment variables.
Rails reads the current environment from the operating system's environment variables by checking the following in order of priority:
RAILS_ENV
environment variable by calling ENV["RAILS_ENV"]
ENV["RACK_ENV"]
"development"
You can see that behavior in the Rails source code by looking at the definition of the Rails.env
method:
def env
@_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
end
Source: https://github.com/rails/rails/blob/4-0-stable/railties/lib/rails.rb#L55-L57
That's the method you call when you write Rails.env
to find out the current environment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With