【发布时间】:2015-05-08 08:23:37
【问题描述】:
如何在 Rails 中切换开发模式和生产模式?
如何将数据库部署到生产环境?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4
如何在 Rails 中切换开发模式和生产模式?
如何将数据库部署到生产环境?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4
如果您使用的是 Rails 4.2,那么您必须知道 Rails 使用“弹簧”来使其更快。因此,在这种情况下,您可以使用以下命令:
对于开发只需运行
Rails 4.2
bin\rails s
Otherwise
rails s
对于生产只需运行
Rails 4.2
bin\rails s -e production
Otherwise
rails s -e production
设置生产数据库 如果生产中的数据库不存在,则运行
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
如果数据库已经存在:
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
另外,如果你想停止 spring 或启动 spring,请使用以下命令:
bin/spring stop
bin/spring start
【讨论】:
使用-e 选项启动服务器。
rails server -e production
而且你不能部署数据库。您需要迁移才能在生产中运行。
【讨论】:
bundle exec rake db:migrate RAILS_ENV=production
RAILS_ENV=production rake db:migrate 也许你还没有创建数据库,所以你必须运行 RAILS_ENV=production rake db:create 和 RAILS_ENV=production rake db:schema:load 并且你的生产数据库将被设置。
puma,请确保 config.public_file_server.enabled = true。
要在开发模式下启动您的服务器,您只需运行rails s,它将以开发模式启动您的应用程序以及您的数据库。
要在生产模式下启动服务器,您需要使用bundle exec rake db:migrate RAILS_ENV=production 迁移数据库,然后使用rails s -e production 或RAILS_ENV=production rails s 在生产中启动服务器
【讨论】:
RAILS_ENV=production rake assets:precompile
在轨道 5+ 转到
config/puma.rb
你可以找到下面一行
environment ENV.fetch("RAILS_ENV") { "development" }
将“开发”改为“生产”
【讨论】:
如果您想在生产环境中运行服务器并在控制台中启用日志,您可以运行:
rails s -C --log-to-stdout -e production
【讨论】: