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

32

我如何在Rails中切换开发模式和生产模式?

我要如何将数据库部署到生产环境?

5个回答

69
如果您正在使用Rails 4.2,则必须知道Rails使用"spring"来加速。因此,在这种情况下,您可以使用以下命令:
开发环境只需运行
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,请使用以下命令:

 bin/spring stop
 bin/spring start

21

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

rails server -e production

你不能直接部署数据库,需要进行迁移以在生产环境中运行。


bundle exec rake db:migrate RAILS_ENV=production - apneadiving
RAILS_ENV=production rake db:migrate 可能你还没有创建数据库,所以你需要运行 RAILS_ENV=production rake db:createRAILS_ENV=production rake db:schema:load 来设置你的生产数据库。 - edariedl
rake db:migrate RAILS_ENV="production" - jon snow
1
在Rails 6中,如果本地使用puma,请确保config.public_file_server.enabled = true - cdmo

6

要在开发模式下启动您的服务器,您只需要运行rails s,它将同时启动您的应用程序和数据库。

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


rake db:setup RAILS_ENV=production 对我有用...但现在我的问题是没有加载样式等.. :( - Felix
这可能是一个与此问题不同的资产问题。尝试谷歌一下,有很多关于这种问题的问答。 - ZazOufUmI
@Felix 请尝试执行RAILS_ENV=production rake assets:precompile - Mohammad AbuShady

5
在Rails 5+中,goto
config/puma.rb 

您可以在下面找到这行代码

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

将"development"改为"production"


当RAILS_ENV环境变量缺失时,这里的"development"是默认值。因此,这个答案并不完全正确 - 最好将RAILS_ENV环境变量设置为"production"而不是在这里更改默认值。 - alexandroid

0

如果您想在生产环境中运行服务器并在控制台中启用日志记录,则可以运行以下命令:

rails s -C --log-to-stdout -e production

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接