繁体   English   中英

ActionView中的Heroku未定义方法错误

[英]Heroku undefined method error in ActionView

Heroku日志显示以下错误:

ActionView::Template::Error (undefined method `accepted_answer_id' for #<Question:0x000000052fd1e0>)

作为背景,我有一个模型Questionhas_many答案。 Question还有一列accepted_answer_id

未定义的方法错误是引用_question.html.erb的一行,其中用于在不同的着色背景中自定义答案计数的显示

  <% if question.answers.count > 0 %> 

    <% if question.accepted_answer_id %>
        <div class="pickedanswer">
          <h4><%= question.answers.count %></h4>
        </div>
    <% else %>
        <div class="numanswer">
          <h4><%= question.answers.count %></h4>
        </div>
    <% end %>

  <% else %>

      <div class="noanswer">
        <h4><%= question.answers.count%></h4>
      </div>

  <% end %>

我认为可能会因为最初的question.accepted_answer_idnil而引发错误..但考虑到我构建逻辑的方式,我不知道为什么它不能简单地跟随(因为它已经在开发中成功完成)。

谢谢你的帮助。

您的架构缺少该属性。 如果您的代码在本地运行,则可以手动添加该列,也可以不在版本控制中进行迁移。

您可以看到迁移的运行情况如下:

$ heroku run rake db:migrate:status --app app_name

这将显示您是否尚未运行任何迁移。 您应该将这些匹配到本地迁移。 你可能错过了吗?

运行db:push破坏您的远程数据库并将其替换为本地副本。 通常你应该使用pgbackups:restore来做这种事情( https://devcenter.heroku.com/articles/heroku-postgres-import-export

我多次遇到过这个问题,这些是我采取的步骤:

  1. 首先确保您已在heroku上迁移数据库:

     heroku run rake db:migrate 
  2. 确保数据库实际上是在heroku上迁移的:

     heroku pg:psql -a app_name \\d table_name 
  3. 最后在heroku上重启app:

     heroku restart 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM