我的测验应用程序中无法指出正确答案和错误答案

时间:2016-06-30 09:09:39

标签: ruby-on-rails ruby

我现在对此一直感到困惑。这是代码:

<ol>
  <% @quiz.questions.each do |question| %>
  <li> <%= question.content  %> </li>


    <% question.answers.each do |answer| %>

     <% @submitted_quiz.submitted_answers.each do |submitted_answer| %> 

    <% if(submitted_answer.content == answer.content && submitted_answer.got_correct) %>
    <li><span class ='correct-answer'><%= answer.content %> CORRECT </span></li>

    <% break %>

    <% elsif(submitted_answer.content == answer.content && submitted_answer.question.id == answer.question.id && submitted_answer.got_incorrect) %>
    <li><span class ='incorrect-answer'><%= answer.content %> INCORRECT</span></li>

    <% break %>

    <% elsif(submitted_answer.content != answer.content) %>
    <li><span class ='normal-answer'> <%= answer.content %> NORMAL</span></li>
    <% break %>

    <% end #  if statement%>

    <% end # submitted_answers.each do%>
    <% end # question.answers.each do |answer|%>

    <% end # @quiz.questions.each do %>

submitted_answers包含用户指示的测验的所有答案都是正确的。例如:

Q: What color is the sun?
Blue
Yellow
Purple
Green

如果用户选择黄色,submitted_answers将在Yellow属性中包含content:string,并将got_correct属性设置为true。如果用户选择Blue,则会发生与以前相同的事情,但现在got_correct仍处于默认状态false,另一个属性got_incorrect设置为{{1 }}。这个的逻辑已经完成,它工作正常。我的问题是,我想显示测验的所有问题和答案,如果答案与truesubmitted_answers的答案匹配,则表明用户通过以绿色突出显示答案来正确回答问题。如果答案与got_correct之一匹配且后者submitted_answers为真,则表示用户通过突出显示红色答案错误地回答了问题。

我的上述代码无法正常工作,因为即使我已将got_incorrectsubmitted_answer匹配,我也会继续迭代submitted_answer。因此,代码适用于第一个问题,并不适用于其余问题。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

试试这个:

<ol>
  <% @quiz.questions.each do |question| %>
  <li> <%= question.content  %> </li>


    <% question.answers.each do |answer| %>

        <% @submitted_quiz.submitted_answers.each do |submitted_answer| %> 

            <% if(submitted_answer.content == answer.content && submitted_answer.got_correct) %>
            <li><span class ='correct-answer'><%= answer.content %> CORRECT </span></li>

            <% break %>

            <% elsif(submitted_answer.content == answer.content && submitted_answer.question.id == answer.question.id && submitted_answer.got_incorrect) %>
            <li><span class ='incorrect-answer'><%= answer.content %> INCORRECT</span></li>

            <% break %>

            <% elsif(submitted_answer.content != answer.content) %>
            <li><span class ='normal-answer'> <%= answer.content %> NORMAL</span></li>
            <% break %>

            <% end #  if statement%>
            <% next %>
        <% end # submitted_answers.each do%>
    <% end # question.answers.each do |answer|%>

  <% end # @quiz.questions.each do %>
</ol>

答案 1 :(得分:0)

您的问题似乎是由于@quiz.questions.answers@submitted_quiz.submitted_answers之间可能称之为错位的原因造成的。对于每个问题,然后是每个正式答案,您将遍历用户响应的每一个。因此,在第一个问题之后,预期结果将会变为break

如果@submitted_quiz.submitted_answers@quiz.questions.answers的并行数组,您可以进行以下更改:

<ol>
  <% @quiz.questions.each do |question| %>
  <li> <%= question.content  %> </li>

    <% question.answers.each_with_index do |answer, index| 
       user_answer = @submitted_quiz.submitted_answers[index]
    %>

    <% if(user_answer.content == answer.content && user_answer.got_correct) %>
    <li><span class ='correct-answer'><%= answer.content %> CORRECT </span></li>

    <% break %>

    <% elsif(user_answer.content == answer.content && user_answer.question.id == answer.question.id && user_answer.got_incorrect) %>
    <li><span class ='incorrect-answer'><%= answer.content %> INCORRECT</span></li>

    <% break %>

    <% elsif(user_answer.content != answer.content) %>
    <li><span class ='normal-answer'> <%= answer.content %> NORMAL</span></li>
    <% break %>

    <% end #  if statement%>

    <% end # question.answers.each do |answer|%>

    <% end # @quiz.questions.each do %>