95

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.

6 Answers 6

254

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  
3
  • 2
    >> Rails.env via rails c appears to display for any mode. Dec 28, 2012 at 19:47
  • 1
    Everything with a questionmark is available. Rails.env is a String decorated with ActiveSupport::StringInquirer. api.rubyonrails.org/classes/ActiveSupport/StringInquirer.html Feb 26, 2015 at 7:59
  • Just as a note, this also works in the rails console. On a production server I was able to run: rails console, then the command above.
    – Ryan
    Sep 9, 2016 at 18:56
39

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?
34

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

3
  • I'm unable to get the second way to work, but there are entries in my production.log saying I created a Task at X time. So why am I not getting the right 404 error page? Do I have to specify somewhere to provide the right error page? Thanks for the help.
    – sent-hil
    Dec 28, 2009 at 4:13
  • I got an error in my view and had to use @Krishnaprasad's approach instead.
    – Besi
    Aug 1, 2013 at 8:17
  • 8
    Please note RAILS_ENV is deprecated. Use Rails.env instead. Apr 1, 2015 at 19:40
8

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

3

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.

3
  • 8
    No one sane is using WEBrick in production.
    – Hauleth
    Sep 18, 2014 at 14:07
  • Hauleth is absolutely correct. This is simply to test what environment you're in while developing.
    – bkunzi01
    Sep 23, 2015 at 14:31
  • If this answer is trolling it's mastery! Congratulations!
    – JD.
    Oct 21, 2016 at 20:05
-2

command line alternative

$echo $RAILS_ENV

2
  • This will not work if the server was started with an inline ENV var. That said, if this was done then a script / alias / etc should live somewhere on-disk, which will include the RAILS_ENV var. One can search the disk for it, such as with ack Jan 9, 2013 at 3:29
  • this does not work.... what is in the variable and how it is getting the environment ???
    – z atef
    Nov 25, 2015 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.