I want to load jQuery UI via Google CDN if in production and locally if in development. So in my application.html.erb layout, I've got to know whether I'm in production or dev. Is there a variable I can check?
Use command ENV in rails console. That will return a hash of your environmental values you can access. Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted.
An environment variable is a key/value pair, it looks like this: KEY=VALUE. We use these variables to share configuration options between all the programs in your computer. That's why it's important to learn how they work & how to access them from your Ruby programs using the ENV special variable.
The ENV hash in your Rails application is set when your Rails application starts. Rails loads into ENV any environment variables stored in your computer and any other key-value pairs you add using Figaro gem.
Rails ships with a default configuration for the three most common environments that all applications need: test, development, and production.
To expand a bit on Paritosh's answer, Rails.env.production?
and Rails.env.development?
will return true/false depending on which environment you're using.
These methods are defined in the StringInquirer
class in the ActiveSupport
module. See them here.
To riff off of the previous answer, you can scope your check to specific environments like so:
Rails.env.development?
where development?
is the name of the environment you want to check.
Also something else I tend to do is if I am checking multiple environments you may want to do something like:
if %w(staging production).include?(Rails.env)
# do something
end
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