我有questions
,其中有五个choices
,其中一个对于choices.is_correct是TRUE。
我目前有一个ERB循环,显示问题和可能的答案选择,并且选择以有序列表(<li>
随机出现)。
在一个单独的ERB循环中,我需要以其相应的<li>
标签(A,B,C,D或E)的形式显示正确答案。
我觉得这可以通过某种局部变量来实现,但是到目前为止,我的测试都还没有工作。局部变量应该起作用,还是我从错误的角度出发?
我在问题/答案部分的当前ERB:
<% @free_questions.each_with_index do |question, i| %>
<div class="row justify-content-center">
<div class="col-lg-10">
<% @a = ("a".."z").to_a %>
<h5>Question <%= i+1 %>: <%= question.name %></h5>
<p>
<ol type="A">
<% @free_choices.where(question: question.id).sort_by{rand}.each do |choice| %>
<li><%= choice.name %></li>
<% end %><br />
</ol>
</p>
<% @free_choices.where(question: question.id, correct: TRUE).each do |choice| %>
<div class="accordion" id="accordionExample">
<% if choice.question.context.present? or choice.question.image.attached? %>
<div class="card">
<div class="card-header" id="headingC<%= i+1 %>">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseC<%= i+1 %>" aria-expanded="true" aria-controls="collapseC<%= i+1 %>">
CLICK FOR QUESTION CONTEXT
</button>
</h5>
</div>
<div id="collapseC<%= i+1 %>" class="collapse" aria-labelledby="headingC<%= i+1 %>" data-parent="#accordionExample">
<div class="card-body">
<p>
<% if choice.question.image.attached? %>
<img src="<%= url_for(choice.question.image) if choice.question.image.attached? %>" class="rounded float-center" alt="context image for this question">
<% end %>
<!--TODO: Add more useful image alts here-->
<%= simple_format(choice.question.context) %>
</p>
</div>
</div>
</div>
<% end %>
<div class="card">
<div class="card-header" id="headingA<%= i+1 %>">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseA<%= i+1 %>" aria-expanded="true" aria-controls="collapseA<%= i+1 %>">
CLICK FOR ANSWER
</button>
</h5>
</div>
<div id="collapseA<%= i+1 %>" class="collapse" aria-labelledby="headingA<%= i+1 %>" data-parent="#accordionExample">
<div class="card-body">
<p>
Correct Answer: <b><%= choice.name %></b>
</p>
<p>
Explanation: <%= raw(choice.question.explanation) %>
</p>
</div>
</div>
</div>
</div>
<% end %>
<hr />
</div>
</div>
<% end %>
编辑:在下面添加模型
question.rb
class Question < ApplicationRecord
before_validation :assign_questionable
belongs_to :questionable, polymorphic: true
has_many :choices, :dependent => :destroy
accepts_nested_attributes_for :choices, allow_destroy: true
choice.rb
class Choice < ApplicationRecord
belongs_to :question
答案 0 :(得分:1)
在这里,我概述了快速修复方法
<% @a = ("a".."z").to_a %>
替换为<% alpha_numbers = ("A".."Z").to_a %>
。您正在使用<ol type="A">
,它以大写形式打印列表,而不是小写。@free_questions.each_with_index
循环外的摘要上方移动,这样您就不必为循环中的每个问题初始化相同的数组。@free_choices.where(question: question.id).sort_by{rand}.each do |choice|
替换为question.choices.shuffle.each_with_index do |choice, index|
。根据模型关系,question.choices
将得到与@free_choices.where(question: question.id)
相同的结果。 shuffle
是做sort_by{rand}
的更好方法。Choice
模型中,添加attr_accessor :alpha_order
。这将创建访问器方法。<li><%= choice.name %></li>
行上方添加<% choice.alpha_order = alpha_numbers[index] %>
。此行在实例变量中设置当前的字母选择顺序。@free_choices.where(question: question.id, correct: TRUE).each do |choice|
替换为question.choices.select { |choice| choice.is_correct }.each do
。对于给定的question
,其中is_correct
是true
,这将仅遍历给定的选择。choice.alpha_order
进行访问,这将给出正确的字母顺序。希望这会有所帮助。