我之前推送到 Heroku 的代码没有问题,但是最后一次推送搞砸了。唯一改变的不是循环遍历 each student,而是循环遍历 each 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'
申请代码
学生.rb
class Student < ActiveRecord::Base
  has_one :user, :as => :profile, dependent: :destroy
...
学生控制器
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 %>
用户.rb
class User < ActiveRecord::Base
  belongs_to :profile, :polymorphic => true
我试过的:
- 仔细检查本地/开发的所有迁移是否与 Heroku 同步
- 克隆 Heroku 文件以仔细检查它们是否运行相同的代码库
- 运行heroku restart命令
- 仔细检查并运行heroku run rake db:migrate以确保一切
- 仔细检查数据库以确保所有数据和列都相同
- 我检查过其他机器和浏览器;还是同样的问题
绝对令人沮丧...感谢您的帮助!