How to tell if rails is in production?

Ruby on-RailsDevelopment EnvironmentProduction Environment

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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  

Solution 2 - Ruby on-Rails

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?

Solution 3 - Ruby on-Rails

2 easy ways:

tail -f log/production.log

if there are entries populating that log after you hit the app, you're in production mode.

second way:

in one of your views (probably the layout is good), just add

<%= "Environment: #{RAILS_ENV}" %>

And that will show you what the environment that you're running in.

edit

You will see the default exception page instead of the actual error pages on any environment if the request is considered "local" (that is from localhost or 127.0.0.1), you can override this by adding this to your ApplicationController

def local_request?
  false
end

You can find this method in the docs in the api

Solution 4 - Ruby on-Rails

On your command line type rails console, then Rails.env.

Solution 5 - Ruby on-Rails

I found it much easier to just restart the rails server and read the second line on the command line:

Running rails s -e production outputs:

=> Booting Puma
=> Rails 4.2.4 application starting in `production` on http://localhost:3000

Had Webrick in the example but some people didn't understand how changing servers would just substitute the name. Updated for clarity.

Solution 6 - Ruby on-Rails

command line alternative

$echo $RAILS_ENV

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsent-hilView Question on Stackoverflow
Solution 1 - Ruby on-RailsKrishna Prasad VarmaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsDogweatherView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDan McNevinView Answer on Stackoverflow
Solution 4 - Ruby on-RailsKarolis RamanauskasView Answer on Stackoverflow
Solution 5 - Ruby on-Railsbkunzi01View Answer on Stackoverflow
Solution 6 - Ruby on-RailsvireshasView Answer on Stackoverflow