Rails - How to Check Developer Mode or Production Mode in Code

Rails - How do I check developer mode or production mode in code

Rails.env == "production"

Rails.env == "development"

As mentioned by @xoebus, this is far better:

Rails.env.production?

Rails.env.development?

How to tell if rails is in production?

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

Rails how to switch between dev and production mode?

If you are using Rails 4.2 then you must know rails uses "spring" to make it faster. So in that case you can use following commands:

For Development just run

Rails 4.2
bin\rails s
Otherwise
rails s

For Production just run

Rails 4.2
bin\rails s -e production
Otherwise
rails s -e production

To setup production database
if database in production does not exist then run

Rails 4.2
bin/rake db:create db:migrate RAILS_ENV=production
Otherwise
rake db:create db:migrate RAILS_ENV=production
bundle exec rake db:create db:migrate RAILS_ENV=production

If DB already exists the:

Rails 4.2
bin/rake db:migrate RAILS_ENV=production
Otherwise
rake db:migrate RAILS_ENV=production
OR
bundle exec rake db:migrate RAILS_ENV=production

Also if you want to stop spring or start spring then use following commands:

 bin/spring stop
bin/spring start

how to change rails to development mode

Though this is not a solution try to start the Rails server this way:

RAILS_ENV=development bundle exec rails s

What do you see if put <%= Rails.env %> somewhere in you layout file?

Rails - error in production mode

In config/environments/production.rb

temporally change

  config.consider_all_requests_local       = false

to

  config.consider_all_requests_local       = true

How to run Padrino project in production mode?

The flag to specify environment in padrino is -e. So the command would be:

padrino start -e production

or

RACK_ENV=production padrino s

Notice how in your case RACK_ENV='production' has quotes around 'production'. You should remove them and try.

From the padrino documentation, other possible combinations are:

# starts the app server (non-daemonized)
$ padrino start
# starts the app server (daemonized) with given port, environment and adapter
$ padrino start -d -p 3000 -e development -a thin

# Stops a daemonized app server
$ padrino stop

# Bootup the Padrino console (irb)
$ padrino console

# Run/List tasks
$ padrino rake

# Run piece of code in the context of Padrino (with given environment)
$ padrino runner 'puts Padrino.env' -e development

# Run Ruby file in the context of Padrino
$ padrino r script/my_script.rb

Check on TEST mode during runtime

Try this for the if condition:

if ENV['RAILS_ENV'] == "test"
# Insert Code Here
end

(replace 'test' with 'development' or 'production' as needed)

Rails code works in development but not in production

You're probably running different database types in development and production (i.e. MySQL in prod and SQLite in dev). Different databases handle Boolean values differently
e.g. 't' vs. 'true' vs. '1'.

I'll bet that if you compare your prod/dev logs you'd see the Boolean values are not being returned as 't' and 'f' in prod, causing your conditional to hit the else every time.

To solve this, make sure you're running the same database in both prod and dev!



Related Topics



Leave a reply



Submit