Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in development or production in ERB file in Rails?

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?

like image 463
at. Avatar asked Sep 28 '12 19:09

at.


People also ask

How do I see environment variables in rails?

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.

What is environment variable in Ruby on Rails?

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.

How is rails ENV set?

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.

What is the default Rails environment?

Rails ships with a default configuration for the three most common environments that all applications need: test, development, and production.


2 Answers

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.

like image 116
Zajn Avatar answered Sep 19 '22 23:09

Zajn


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
like image 29
Matt Polito Avatar answered Sep 19 '22 23:09

Matt Polito