注释一旦部署到Heroku,将无法正常工作

问题描述

我最近在Heroku上部署了一个应用程序,但是我的评论有问题。它在开发中工作正常,但在生产中出现以下错误。看来Heroku的我的comments.index.html.erb文件有问题(也包括在下面),有人能指出我正确的方向吗?就是说comment.recipe_id不存在,但我不明白为什么它在开发中会很好,但是一旦部署就不会呢?谢谢!

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>

解决方法

部署到heroku之后,您必须进行迁移。希望您之前对自己的gem文件进行过更改。 Heroku正在与postgres合作,并且开发正在使用sqlite。如果您以前没有进行过更改,它将无法正常工作。

在生产组中,您必须添加postgres gem:

group :production do
  gem 'pg'
end

然后运行bundle install-不进行生产。现在您可以部署了。完成后,再次转到您的终端并输入:

heroku run rake db:migrate

现在,如果没有其他错误,它应该可以工作