得到奇怪的'未定义方法'错误

Luk*_*ych 2 ruby ruby-on-rails

为#<Mark:0x00000001e057d0>获取"undefined method` each'"

这是观点:

<% @current_marks.each do |m| %>
  <tr>
    <td class='col-md-3'><%= m.subject %></td>
    <td class='col-md-3'><%= m.professor %></td>
    <td class='col-md-3'><%= m.semester %></td>
    <td class='col-md-3'><%= m.points %></td>
  </tr>
<% end %>
Run Code Online (Sandbox Code Playgroud)

和控制器:

def show
  @student = Student.find(params[:id])
  @students = Student.all
  @current_marks = Mark.find_by! student_id: @student.id
rescue ActiveRecord::RecordNotFound
  redirect_to :action => 'set_marks'
end
Run Code Online (Sandbox Code Playgroud)

我已经检查了一个Id param并且它是正确的.我Mark也在DB中有正确的记录student_id.如何在@current_marks没有任何错误的情况下致电?

pun*_*cse 7

find_by 会给你第一个匹配记录不是一个集合,所以你不能把每个上that.Instead你可以使用:如果你需要的所有符合条件的记录

 @current_marks = Mark.where student_id: @student.id
Run Code Online (Sandbox Code Playgroud)