繁体   English   中英

ActionView :: Template :: Error(未定义方法…)在本地工作,但不适用于Heroku

[英]ActionView::Template::Error (undefined method…) Works locally, but not on Heroku

我对之前推送到Heroku的代码没有任何麻烦,但是最后一次推送已经搞砸了。 唯一改变的是,它不再遍历每个student ,而是遍历每个user

背景

该代码在本地有效,但在Heroku上无效。 在Heroku上引发错误的页面是所有学生的列表(索引)。 什么代码正在做的是通过所有的循环Users有一个profile_type = "Student"

由于某种原因,它试图访问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

我试过的

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

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

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

我将问题缩小到config.eager_load 它最初设置为true ,但是将其更改为config.eager_load = false修复了该问题。

仍然不确定为什么问题始终存在,但现在已解决!

我遇到了同样的问题,并将config.eager_load设置为true解决此问题。 但是,在生产环境中不建议使用此设置,因此我试图找出真正的问题所在。

我终于意识到这是由于其他一些模型类的设置不正确(它仍在开发中), 即使它与错误模型绝对无关 config.eager_load选项设置为true ,出于优化原因,Rails会在启动时加载所有类。 如果某些模型类不正确,那么这将使事情变得混乱,并且关系可能变得怪异。

一旦我删除了错误的/不完整的模型类,一切都会重新开始。

暂无
暂无

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

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