【问题标题】:Rails how to switch between dev and production mode?Rails 如何在开发模式和生产模式之间切换?
【发布时间】:2015-05-08 08:23:37
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

    如果您使用的是 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
    

    【讨论】:

      【解决方案2】:

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

      rails server -e production
      

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

      【讨论】:

      • bundle exec rake db:migrate RAILS_ENV=production
      • RAILS_ENV=production rake db:migrate 也许你还没有创建数据库,所以你必须运行 RAILS_ENV=production rake db:createRAILS_ENV=production rake db:schema:load 并且你的生产数据库将被设置。
      • rake db:migrate RAILS_ENV="production"
      • 在 Rails 6 中,如果在本地使用 puma,请确保 config.public_file_server.enabled = true
      【解决方案3】:

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

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

      【讨论】:

      • rake db:setup RAILS_ENV=production 为我工作......但现在我遇到的问题是没有加载样式等.. :(
      • 这可能是与此问题不同的资产问题。尝试google一下,这类问题有很多问题/答案
      • @Felix 试试RAILS_ENV=production rake assets:precompile
      【解决方案4】:

      在轨道 5+ 转到

      config/puma.rb 
      

      你可以找到下面一行

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

      将“开发”改为“生产”

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-02-04
          • 1970-01-01
          • 2013-12-14
          • 2011-02-28
          • 2021-01-17
          • 2018-05-01
          • 2020-02-24
          • 2017-12-21
          相关资源
          最近更新 更多