Search code examples
ruby-on-railsherokuerb

Comments not working once deployed to Heroku


I have recently deployed an app to Heroku and I am having an issue with my comments. It's working fine in development but in production I'm getting the error below. It looks like Heroku is having an issue with my comments.index.html.erb file (also included below) Can anyone point me in the right direction? It is saying comments.recipe_id does not exist but I don't understand why it would be fine in development but not once deployed? Thanks!

2020-09-22T03:42:18.459074+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf] ActionView::Template::Error (PG::UndefinedColumn: ERROR:  column comments.recipe_id does not exist
2020-09-22T03:42:18.459075+00:00 app[web.1]: LINE 1: SELECT "comments".* FROM "comments" WHERE "comments"."recipe...
2020-09-22T03:42:18.459076+00:00 app[web.1]: ^
2020-09-22T03:42:18.459076+00:00 app[web.1]: HINT:  Perhaps you meant to reference the column "comments.recipe".
2020-09-22T03:42:18.459077+00:00 app[web.1]: ):
2020-09-22T03:42:18.459077+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf]      5: <% end %>
2020-09-22T03:42:18.459078+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf]      6:
2020-09-22T03:42:18.459078+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf]      7: <ul>
2020-09-22T03:42:18.459079+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf]      8: <% @comments.each do |c|%>
2020-09-22T03:42:18.459081+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf]      9:   <li><%= c.user.username%> says "<%= c.content%>" about this recipe: <strong><%= link_to c.recipe.title, recipe_path(c.recipe_id) %></strong></li>
2020-09-22T03:42:18.459082+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf]     10:
2020-09-22T03:42:18.459082+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf]     11: <% end %>
2020-09-22T03:42:18.459082+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf]
2020-09-22T03:42:18.459083+00:00 app[web.1]: [0304f2e5-aabb-48a2-8038-734c995042cf] app/views/comments/index.html.erb:8
2020-09-22T03:42:18.459389+00:00 heroku[router]: at=info method=GET path="/recipes/1/comments" host=myrailsrecipeapp.herokuapp.com request_id=0304f2e5-aabb-48a2-8038-734c995042cf fwd="174.97.92.123" dyno=web.1 connect=0ms service=12ms status=500 bytes=1827 protocol=https```

comments/index.html.erb

<% if @recipe %>
  <h1>Comments for <%= @recipe.title%></h1>
<% else %>
  <h1>All Comments</h1>
<% end %>

<ul>
  <% @comments.each do |c|%>
    <li><%= c.user.username%> says "<%= c.content%>" about this recipe: <strong><%= link_to c.recipe.title, recipe_path(c.recipe_id) %></strong></li>
  <% end %>
</ul>

Solution

  • You have to do a migration after deploying to heroku. I hope you made changes in your gem file before. Heroku is working with postgres and development is using sqlite. If you havent make the changes before, it wont work.

    In the production group you have to add the postgres gem:

    group :production do
      gem 'pg'
    end
    

    Then run bundle install --without production. Now you can deploy. When its finished go to your terminal again and enter:

    heroku run rake db:migrate
    

    Now it should work if there are no other errors