Rails how to switch between dev and production mod

2020-05-14 09:50发布

问题:

How can I switch in Rails between the dev mode and the production mode?

and how can I deploy the database to production?

回答1:

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


回答2:

Start server using -e option.

rails server -e production

And you can not deploy database. you needs migrations to run in production.



回答3:

To start your server in development mode you only need to run rails s it will start your app in dev mode as well as your database.

To start your server in production mode you need to migrate your database with bundle exec rake db:migrate RAILS_ENV=production and then start your server in production using rails s -e production or RAILS_ENV=production rails s



回答4:

In rails 5+ goto

config/puma.rb 

You can find the below line

environment ENV.fetch("RAILS_ENV") { "development" }

change "development" to "production"