问题
I didn't have trouble on previous code pushed to Heroku, but this last push has messed up. The only thing that has changed is instead of looping through each student, it's now looping through each user.
Background
The code works locally, but not on Heroku. The page that is raising an error on Heroku is a list (index) of all the students. What the code is doing is looping through all Users that have a profile_type = "Student".
For some reason it's trying to access the polymorphic association (profile) on a Student object, when the User object should be used instead.
Log from 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'
Application Code
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 for students
<% @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
What I have tried:
- Double checked that all migrations from local/dev are in sync with Heroku
- Cloned the Heroku files to double check that they are running the same codebase
- Ran the
heroku restartcommand - Double checked and ran
heroku run rake db:migrateto make sure everything - Double checked the database to make sure all data and columns are the same
- I've checked on other machines and browsers; still the same issue
Definitely frustrating... Thanks for any help!
回答1:
Thanks to Leo Correa's suggestion, I started the rails server in production mode and was able to reproduce the error. (I used the RAILS_ENV=production rails s to launch the server locally in production mode.)
I narrowed the issue down to config.eager_load. It was originally set to true, but changing it to config.eager_load = false fixed the issue.
Still not sure why the issue persisted in the first place, but it's fixed now!
回答2:
I had the same issue and setting config.eager_load to true fixed it. However this is not a recommended setting in production, so I tried to find out what was really the issue.
I finally realised that it was because of some other model class that was incorrectly set up (it was still under development), even though it had absolutely nothing to do with the erroring models. When the config.eager_load option is set to true it causes Rails to load all classes at startup for optimisation reasons. If some model classes are incorrect, then this causes things to be messed up and the relations may become weird.
As soon as I deleted the wrong/incomplete model class, everything started to work again.
来源:https://stackoverflow.com/questions/18804564/actionviewtemplateerror-undefined-method-works-locally-but-not-on-hero