19

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?

at.
  • 50,922
  • 104
  • 292
  • 461

3 Answers3

38

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.

Zajn
  • 4,078
  • 24
  • 39
  • 1
    Where can I see the source of these functions? must be a meta programming thing.any idea? – shajin Jan 28 '14 at 08:25
  • 1
    Yes, it is. Have a look [here](https://github.com/rails/rails/blob/e20dd73df42d63b206d221e2258cc6dc7b1e6068/activesupport/lib/active_support/string_inquirer.rb). – Zajn Jan 28 '14 at 14:43
9

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
Matt Polito
  • 9,588
  • 2
  • 19
  • 13
2

use Rails.env, it will give you environment you are working on.

Kyle
  • 21,978
  • 2
  • 60
  • 61
Paritosh Singh
  • 6,288
  • 5
  • 37
  • 56