Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if rails is in production?

I used script/server -e production to start rails in production mode. It did and I got no errors. However how do I tell if it is in production mode? I tried a non-existent route, and I got a similar error page I did in development.

I thought if under production model, I get the 404 error page that is in my /public folder.

Does it mean it didn't start in production mode?

Thanks for your help.

like image 894
sent-hil Avatar asked Dec 28 '09 03:12

sent-hil


People also ask

How do I check environment in Ruby?

Ruby has direct access to environment variables via the ENV hash. Environment variables can be directly read or written to by using the index operator with a string argument.

What is Rails ENV?

Here we can see that the Rails. env object is an instance of the class ActiveSupport::StringInquirer . This class inherits from String . So the Rails. env object is a string that represents the current environment.


2 Answers

If its Rails 3.1+, Rails.env.production? will return true when in production.

Rails.env.production?  #=> true   Rails.env.staging?     #=> false Rails.env.development? #=> false   
like image 101
Krishna Prasad Varma Avatar answered Sep 18 '22 12:09

Krishna Prasad Varma


For modern Rails versions (3+), Rails.env returns the environment as a String:

Rails.env #=> "production" 

There are also helpful accessors* for each environment that will return a Boolean:

Rails.env.production?  #=> true   Rails.env.staging?     #=> false Rails.env.development? #=> false 

(*) There's a gotcha here: these aren't real accessors. They're just strings of letters and if they happen to match the current environment name, they return true. They fail silently. That means that you can be in production, but if have a typo in this code, you won't get an error, you'll simply get false:

Rails.env.producton?  #=> false 

For that reason, I set constants in an initializer and only refer to those in the rest of my code. This lets the Ruby interpreter help me catch my own errors:

PRODUCTION = Rails.env.production? DEVELOPMENT = Rails.env.development? TEST = Rails.env.test? 
like image 40
Dogweather Avatar answered Sep 20 '22 12:09

Dogweather