[英]heroku ActionView::Template::Error (undefined method `email'
[英]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
我試過的
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.