Search code examples
ruby-on-railsdatabaseherokudeploymentruby-on-rails-5

How to fix: "Items from database are missing after deploying on Heroku"


After deployment of the Web Application (online store) written in Ruby On Rails on Heroku the products from the database are not displayed. I deployed and I migrated the database using 'git push heroku master' and then 'heroku run rails db:migrate'. I'm using PostgreSQL and Paperclip gem for image upload.

I tried to upload a new products from deployed application and it works.

                <% @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 %>       

I expected the display of items from the database, but after migration, the products are still missing and i can only add them from deployed application.


Solution

  • Your databases are by default completely independent between your various Rails environments: anything you do on your local machine exists only your local machine, not on Heroku (and vice-versa). (After all, you wouldn't want your test products showing up in your live store.)

    If you have specific database records that always need to be present — whether you're on your local development machine, or in your test environment, or in production on Heroku — that's why the Active Record "seed" feature exists.