【问题标题】:How to fix: "Items from database are missing after deploying on Heroku"如何解决:“在 Heroku 上部署后,数据库中的项目丢失”
【发布时间】:2019-10-12 14:15:17
【问题描述】:

在 Heroku 上部署用 Ruby On Rails 编写的 Web 应用程序(在线商店)后,不会显示数据库中的产品。我部署并使用“git push heroku master”迁移数据库,然后使用“heroku run rails db:migrate”。我正在使用 PostgreSQL 和 Paperclip gem 上传图片。

我尝试从已部署的应用程序上传新产品,它可以正常工作。

                <% @products.each do |product| %>
                    <div class="col-lg-4 col-md-6 mb-4">
                        <div class="cart h-100">
                            <% if product.image.present? %>
                                <%= link_to image_tag(product.image.url(:thumb)), product_path(product)%>
                            <% end %>
                            <div class="card-body">
                                <h4 class="card-title"><%= product.name %></h4>
                                <h5><%= product.price %></h5>
                                <p class="card-text"><%= product.description %></p>
                            </div>
                            <div class="card-footer">
                                <% if current_user && current_user.admin? %>
                                    <%= link_to 'Edit', edit_product_path(product) %>
                                    <%= link_to 'Delete', product_path(product), method: :delete, data: { confirm: 'Are you sure?' } %>
                                <% elsif current_user && !current_user.admin %>
                                    <%= form_tag(line_items_path(product_id: product.id)) do %>
                                        <%= number_field_tag(:quantity, 1) %>
                                        <%= submit_tag('Add to shopping cart') %>
                                    <% end %>
                                <% elsif !user_signed_in? %>
                                <% end %>
                            </div>
                        </div>
                    </div>
                <% end %>       

我希望显示来自数据库的项目,但迁移后,产品仍然丢失,我只能从部署的应用程序中添加它们。

【问题讨论】:

  • "我希望显示数据库中的项目,但迁移后,产品仍然丢失,我只能从部署的应用程序中添加它们。"什么迁移?您希望这些产品从哪里出现?
  • 您不希望本地/开发环境中的products 出现在您的 Heroku/生产环境中,是吗?
  • 问题编号。 1 是我无法从本地环境迁移/显示我的产品。问题编号。 2是一旦从部署的应用程序添加product,文本数据只出现图像不显示。这是否意味着我需要另一个位置来存储我的文件?如果是,你会推荐什么?感谢您的帮助!
  • 我阅读了这篇关于图像上传的文章devcenter.heroku.com/articles/…,但我在配置方面遇到了一些问题,同时我找到了另一个解决方案:Rails 5 的 Active Storage - 我会尝试实现它跨度>

标签: ruby-on-rails database heroku deployment ruby-on-rails-5


【解决方案1】:

默认情况下,您的数据库在各种 Rails 环境之间完全独立:您在本地机器上所做的任何事情都只存在于您的本地机器上,而不存在于 Heroku 上(反之亦然)。 (毕竟,您不希望您的测试产品出现在您的直播商店中。)

如果您有始终需要存在的特定数据库记录(无论您是在本地开发机器上,还是在测试环境中,还是在 Heroku 上的生产环境中),这就是存在 Active Record "seed" feature 的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-15
    • 2022-01-25
    • 2021-05-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    相关资源
    最近更新 更多