简体   繁体   English

参数个数错误(0表示1)

[英]wrong number of arguments (0 for 1)

In my quiz i have created a questions table with numbers it is used for whether the question is answered or not. 在我的测验中,我创建了一个带有数字的问题表,用于表示问题是否得到回答。 if it answered the visited column in answer table goes to 1 or else 0 as 如果它回答了答案表中的访问列,则转到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 |
+----+--------+--------------+---------+---------------+------------+-------+---------+

and i checked the questions based on visited in my view page as like 我在我的视图页面中根据访问情况检查了问题

<% @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>

but it gives the error as wrong number of arguments (0 for 1) 但它将错误作为错误的参数数量(0表示1)

Answer.find_by_sql is wrong! Answer.find_by_sql错了! Use space before [] or () 在[]或()之前使用空格

<% @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>

And you don't need to use @slno - use each_with_index 而且您不需要使用@slno - 使用each_with_index

refactored code: 重构代码:

<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>

You need to wrap your find_by_sql in () like so: 您需要将find_by_sql包装在()中,如下所示:

Answer.find_by_sql(["SELECT visited from answers where questions_id=#{s.id}"])

you also don't need to write your own SQL in this instance, instead you can simply do this: 您也不需要在此实例中编写自己的SQL,而只需执行此操作:

<% if Answer.where(questions_id: s.id).count == 1 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM