我有这些模型
class Course < ActiveRecord::Base
attr_accessible :name
has_many :teachers
end
class Teacher < ActiveRecord::Base
attr_accessible :id, :name, :course_id
belongs_to :course
has_many :evaluations
end
class Evaluation < ActiveRecord::Base
attr_accessible :teacher_id, :course_id
belongs_to :teacher
end
这是视图/评估/index.html.erb 文件
<% @evaluations.each do |evaluation| %>
<tr>
<td><%= evaluation.teacher_id %></td>
<td><%= link_to 'Show', evaluation %></td>
<td><%= link_to 'Edit', edit_evaluation_path(evaluation) %></td>
<td><%= link_to 'Destroy', evaluation, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
<% end %>
我想显示老师的名字:
<td><%= evaluation.teacher.name %></td>
但它不起作用。Rails 显示此错误:
"undefined method `name' for nil:NilClass"
谁能帮我?