[英]wrong number of arguments (0 for 1)
在我的測驗中,我創建了一個帶有數字的問題表,用於表示問題是否得到回答。 如果它回答了答案表中的訪問列,則轉到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>
您需要將find_by_sql包裝在()中,如下所示:
Answer.find_by_sql(["SELECT visited from answers where questions_id=#{s.id}"])
您也不需要在此實例中編寫自己的SQL,而只需執行此操作:
<% if Answer.where(questions_id: s.id).count == 1 %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.