【问题标题】:ActionView::Template::Error (undefined method...) Works locally, but not on HerokuActionView::Template::Error (undefined method...) 在本地工作,但不在 Heroku 上
【发布时间】:2014-03-09 20:52:22
【问题描述】:

我之前推送到 Heroku 的代码没有问题,但是最后一次推送搞砸了。唯一改变的不是循环遍历每个student,而是循环遍历每个user

背景

代码在本地工作,但在 Heroku 上不工作。在 Heroku 上引发错误的页面是所有学生的列表(索引)。代码所做的是遍历所有具有profile_type = "Student"Users

由于某种原因,它试图访问 Student 对象上的多态关联(配置文件),而应使用 User 对象。

来自 Heroku 的日志

ActionView::Template::Error (undefined method `profile' for #<Student:0x007f80c5552330>):
35:         <tbody>
36:          <% @students.each do |user| %>
37:           <tr>
38:             <td><%= link_to user.profile.ivywise_id, student_path(user.profile_id) %></td>
39:             <td><%= link_to user.first_name.camelize, student_path(user.profile_id) %></td>
40:             <td><%= link_to user.last_name.camelize, student_path(user.profile_id) %></td>
41:             <td><%= user.email %></td>
app/views/students/index.html.erb:38:in `block in_app_views_students_index_html_erb__3704269538007702833_70095521176320'
app/views/students/index.html.erb:36:in `_app_views_students_index_html_erb__3704269538007702833_70095521176320'

应用程序代码

student.rb

class Student < ActiveRecord::Base
  has_one :user, :as => :profile, dependent: :destroy
...

students_controller

def index
  @students = User.where(profile_type: "Student").order("last_name")
end

针对学生的 index.html.erb

  <% @students.each do |user| %>
  <tr>
    <td><%= link_to user.profile.ivywise_id, student_path(user.profile_id) %></td>
    <td><%= link_to user.first_name.camelize, student_path(user.profile_id) %></td>
    <td><%= link_to user.last_name.camelize, student_path(user.profile_id) %></td>
    <td><%= user.email %></td>
    <td></td>
    <td>
      <%= link_to "Edit", edit_student_path(user.profile_id), class: "btn btn-default btn-small" if can? :edit, Student %>
    </td>
  </tr>
  <% end %>

user.rb

class User < ActiveRecord::Base
  belongs_to :profile, :polymorphic => true

我尝试过的:

  • 双重检查本地/开发的所有迁移都与 Heroku 同步
  • 克隆 Heroku 文件以仔细检查它们是否运行相同的代码库
  • 运行heroku restart 命令
  • 仔细检查并运行 heroku run rake db:migrate 以确保一切正常
  • 仔细检查数据库以确保所有数据和列都相同
  • 我在其他机器和浏览器上检查过;还是一样的问题

绝对令人沮丧...感谢您的帮助!

【问题讨论】:

  • 您是否尝试过以生产模式在本地启动应用程序并查看是否遇到相同的错误?
  • 太棒了!感谢您的建议...没想到在生产模式下对其进行测试。至少现在我能够重现问题并缩小范围。
  • 您确定您没有更改任何其他内容吗?数据类型显示为Student,它表示该类型的变量没有方法profile

标签: ruby-on-rails heroku ruby-on-rails-4


【解决方案1】:

感谢 Leo Correa 的建议,我在生产模式下启动了 rails 服务器并能够重现错误。 (我使用RAILS_ENV=production rails s 以生产模式在本地启动服务器。)

我将问题缩小到config.eager_load。它最初设置为true,但将其更改为config.eager_load = false 解决了这个问题。

仍然不确定为什么问题仍然存在,但现在已修复!

【讨论】:

  • 我遇到了完全相同的问题,我以同样的方式解决了问题,顺便说一句,谢谢。知道为什么会这样吗?我应该改回config.eager_load = true 吗?
【解决方案2】:

我遇到了同样的问题,将config.eager_load 设置为true 修复了它。但是,这不是生产中的推荐设置,所以我试图找出真正的问题。

我终于意识到这是因为其他一些模型类设置不正确(它仍在开发中),尽管它与错误模型完全无关。当 config.eager_load 选项设置为 true 时,它会导致 Rails 在启动时加载所有类以进行优化。如果某些模型类不正确,则会导致事情变得混乱,关系可能会变得奇怪。

一旦我删除了错误/不完整的模型类,一切又开始工作了。

【讨论】:

  • 我根据您的建议再次查看并查看了所有模型类,但一切似乎都设置得很好。你的模型是什么样的?只要模型类继承自 ActiveRecord::Base 就可以了。
  • 我有另一个模型不完整,并且与失败的模型有关。代码中的任何地方都没有使用不完整的模型,但由于 config.eager_load,它在启动时被加载。不完整是指关系设置不正确。它在本地工作,因为模型从未加载,但不在加载它的 prod 环境中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 2015-04-15
相关资源
最近更新 更多