我试图根据是否选择了正确的问题答案来更改引导程序列表项的颜色......但使用link_to会使其变得复杂。建议表示赞赏。 THX。
<ul class="list-group" id="answered">
<% @question.answers.each do |answer| %>
<%= link_to(answer.text, question_path(@question, attempted_answer: answer.id), :class => "list-group-item") %>
<% if @attempted_answer && @attempted_answer == answer %>
<% if @attempted_answer.correct %>
correct
<% else %>
incorrect
<% end %>
<% end %>
答案 0 :(得分:2)
def correct_answer(attempted_answer, answer)
return false unless attempted_answer
attempted_answer == answer && attempted_answer.correct
end
<%= link_to(answer.text, question_path(@question, attempted_answer: answer.id), :class => "list-group-item #{correct_answer?(@attempted_answer, answer) ? 'list-group-item-success' : 'list-group-item-danger'}") %>
<% correct_answer?(@attempted_answer, answer) ? 'correct' : incorrect %>
<% end %>