내 퀴즈에서 나는 질문에 대한 답변 여부에 사용되는 숫자로 질문 테이블을 만들었습니다. 답변 테이블의 방문 열에 응답하면 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>
그러나 잘못된 수의 인수 로 오류를 제공합니다 (1에 대해 0)
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] 삭제
몇 마디 만하겠습니다