私はクイズを作成し、シャッフルを使用して表示する質問をランダム化する必要がありますが、次の関数を呼び出して次の質問を表示します。順序が再びシャッフルされましたコントローラーが開始する前にシャッフルされた順序が必要であり、そのコントローラーのすべてのアクションでそれを保持します
private
def shuffle(exam_group_id,student_additional_field_id)
questions = Question.find(:all, :conditions => ['exam_group_id=? && student_additional_field_id=?',exam_group_id,student_additional_field_id]).flatten.shuffle
end
ここで次の関数を使用します
class AnswersController < ApplicationController
def start
@user = current_user
@student = Student.find_by_admission_no(@user.username)
@exam_group = ExamGroup.find_by_id(params[:exam_group_id])
@answer = Answer.new(params[:ans])
@module = params[:student_additional_field]
@questions = shuffle(@exam_group,@module)
@ques = []
@questions.each do |a|
@ques.push a.id unless a.id.nil?
end
a = @ques[0]
@s = 1
@ans = Question.find_by_id(a)
render(:update) do |page|
page.replace_html 'main', :partial => 'ans', :object => @ans
page.replace_html 'quespan', :partial => 'ques'
end
end
def next
@user = current_user
@student = Student.find_by_admission_no(@user.username)
@exam_group = ExamGroup.find_by_id(params[:exam_group_id])
@answer = Answer.new(params[:ans])
@answer.answer = params[:answer]
unless params[:answer].nil?
@answer.visited = 1
else
@answer.visited = 0
end
@answer.exam_group_id = @exam_group.id
@answer.user_id = @user.id
passed_question = params[:passed_question]
@answer.questions_id = passed_question
@question = Question.find_by_id(passed_question)
@module = Question.find_by_sql ["SELECT student_additional_field_id FROM questions WHERE id=#{passed_question}"]
student_additional_field_id = @module[0].student_additional_field_id
@s = 1
@questions = shuffle(@exam_group,student_additional_field_id)
@ques = []
@questions.each do |a|
@ques.push a.id unless a.id.nil?
end
a = @ques[0]
@answer.modules_id = student_additional_field_id
if params[:answer] == @question.is_answer
@answer.marks = 1
else
@answer.marks = 0
end
if @answer.save
@ans = Question.find_by_id(a, :conditions => [' id not in (?)',answered])
@s = @s + answered.count
unless @ans.nil?
render(:update) do |page|
page.replace_html 'main', :partial => 'ans', :object => @ans
end
else
render(:update) do |page|
page.replace_html 'main', :partial => 'ans2'
end
end
end
end
私を助けてください