在我的测验中,我创建了一个带有数字的问题表,用于查询问题是否得到回答。如果它回答了答案表中的访问列,则为1,否则为0
+----+--------+--------------+---------+---------------+------------+-------+---------+
| id | answer | questions_id | user_id | exam_group_id | modules_id | marks | visited |
+----+--------+--------------+---------+---------------+------------+-------+---------+
| 1 | ans2 | 8 | 3 | 1 | 1 | 0 | 1 |
| 2 | NULL | 9 | 3 | 1 | 1 | 0 | 0 |
| 3 | NULL | 6 | 3 | 1 | 1 | 0 | 0 |
| 4 | ans1 | 5 | 3 | 1 | 2 | 1 | 1 |
| 5 | NULL | 4 | 3 | 1 | 2 | 0 | 0 |
| 6 | NULL | 3 | 3 | 1 | 2 | 0 | 0 |
+----+--------+--------------+---------+---------------+------------+-------+---------+
然后我根据查看页面中访问过的内容检查了问题,就像
<% @slno = 0 %>
<ul class="student_list">
<% @questions.each do |s| %>
<% @slno = @slno + 1 %>
<% if ((Answer.find_by_sql["SELECT visited from answers where questions_id=#{s.id}"]) == 1) %>
<li class="student_names">
<a href="#" id="<%=s.id%>" class="student-link" > <%= @slno %></a>
</li>
<% else %>
<li class="student_names2">
<a href="#" id="<%=s.id%>" class="student-link2" > <%= @slno %></a>
</li>
<% end %>
<% end %>
</ul>
但它给出错误的参数数量错误(0表示1)
Answer.find_by_sql是错误的!在[]之前使用空格或()
<% @slno = 0 %>
<ul class="student_list">
<% @questions.each do |s| %>
<% @slno = @slno + 1 %>
<% if ((Answer.find_by_sql(["SELECT visited from answers where questions_id=#{s.id}"])) == 1) %>
<li class="student_names">
<a href="#" id="<%=s.id%>" class="student-link" > <%= @slno %></a>
</li>
<% else %>
<li class="student_names2">
<a href="#" id="<%=s.id%>" class="student-link2" > <%= @slno %></a>
</li>
<% end %>
<% end %>
</ul>
而且您不需要使用@slno
-使用each_with_index
重构代码:
<ul class="student_list">
<% @questions.each_with_index do |s, index| %>
<% if ((Answer.find_by_sql(["SELECT visited from answers where questions_id=#{s.id}"])) == 1) %>
<li class="student_names">
<a href="#" id="<%=s.id%>" class="student-link" > <%= index %></a>
</li>
<% else %>
<li class="student_names2">
<a href="#" id="<%=s.id%>" class="student-link2" > <%= index %></a>
</li>
<% end %>
<% end %>
</ul>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句