Rails如何在开发和生产模式之间切换?

Fel*_*lix 25 ruby ruby-on-rails ruby-on-rails-4

如何在开发模式和生产模式之间切换Rails?

以及如何将数据库部署到生产中?

shi*_*ret 57

如果您使用的是Rails 4.2,那么您必须知道rails使用"spring"来加快速度.那么在这种情况下,您可以使用以下命令:

对于开发只是运行

Rails 4.2
    bin\rails s
Otherwise
   rails s
Run Code Online (Sandbox Code Playgroud)

对于生产只是运行

Rails 4.2
    bin\rails s -e production
Otherwise    
    rails s -e production
Run Code Online (Sandbox Code Playgroud)

如果生产中的数据库不存在则设置生产数据库然后运行

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
Run Code Online (Sandbox Code Playgroud)

如果DB已经存在:

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
Run Code Online (Sandbox Code Playgroud)

此外,如果您想要停止弹簧或启动弹簧,请使用以下命令:

 bin/spring stop
 bin/spring start
Run Code Online (Sandbox Code Playgroud)


jon*_*now 15

使用-e选项启动服务器.

rails server -e production
Run Code Online (Sandbox Code Playgroud)

而且你无法部署数据库.您需要迁移才能在生产中运行.


Sté*_*ane 6

要以开发模式启动服务器,只需运行 rails s它启动应用程序以及数据库。

要以生产模式启动服务器,您需要使用迁移数据库,bundle exec rake db:migrate RAILS_ENV=production然后使用rails s -e production或在生产环境中启动服务器。RAILS_ENV=production rails s


Ali*_*bar 5

在 Rails 5+ 中转到

config/puma.rb 
Run Code Online (Sandbox Code Playgroud)

您可以找到以下行

environment ENV.fetch("RAILS_ENV") { "development" }
Run Code Online (Sandbox Code Playgroud)

变“开发”为“生产”