我刚刚开始,发现了一个我无法解决的问题。我正在创建一个带有两个模型(奖项和学生)的简单应用程序。:award has_one :student
和:student belongs_to :award
我被告知在 app/views/awards/ show.html.erb 中,替换:
<%= @award.student_id %>
和:
<%= @award.student.given_name %> <%= @award.student.family_name %>
在 index.html.erb 中,替换:
<%= award.student_id %>
和:
<%= award.student.given_name %> <%= award.student.family_name %>
在此之后 localhost:3000/students 工作正常,但 localhost:3000/awards 不工作并给出以下错误:-
**NoMethodError in Awards#index**
Showing /home/redblink/rbtest/students/app/views/awards/index.html.erb where line #17 raised:
undefined method `given_name' for nil:NilClass
Extracted source (around line #17):
14: <tr>
15: <td><%= award.name %></td>
16: <td><%= award.year %></td>
17: <td><%= award.student.given_name %> <%= award.student.family_name %></td>
18: <td><%= link_to 'Show', award %></td>
19: <td><%= link_to 'Edit', edit_award_path(award) %></td>
20: <td><%= link_to 'Destroy', award, method: :delete, data: { confirm: 'Are you sure?' } %></td>
请尽快回复解决方案.....