【发布时间】: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