2016-06-30 131 views
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%> 

    <% 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將包含Yellowcontent:string屬性,同時還設置got_correct屬性true。如果用戶選擇Blue,則同樣的情況會發生,如前所述got_correct保持其默認狀態false,而另一個屬性got_incorrect設置爲true。這個邏輯已經完成,它工作正常。我的問題是,我想顯示測驗的所有問題和答案,如果答案與submitted_answersgot_correct的答案匹配爲真,我表示用戶通過以綠色突出顯示答案來正確回答了問題。如果答案與submitted_answers之一匹配,而後者的got_incorrect爲真,則表明用戶錯誤地以紅色突出顯示答案來回答問題。

我上面的代碼不起作用,因爲即使在我將submitted_answer匹配到answer後,我仍然在迭代submitted_answer。所以,代碼適用於第一個問題,並且不適用於其餘問題。我應該如何解決這個問題?

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