0

我创建了一个测验,并且必须使用随机播放随机化要显示的问题,但是当我调用下一个函数来显示下一个问题时。订单再次洗牌我只想要控制器启动之前的洗牌顺序并在该控制器的所有操作中保留它

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

请帮帮我

4

2 回答 2

1

您可以传递shuffle种子,它将始终返回结果。您可以将此种子作为参数传递给您的next操作。您可以在此处阅读有关它的更多信息:重现随机数组排序

修改你的shuffle方法以接受一个可选的种子参数,然后你可以在你的控制器中传递它,来自一个参数。

于 2013-11-13T04:44:45.503 回答
0

根据有多少问题,我会随机播放开始操作中的问题并将顺序保存到会话或问题表单上的隐藏字段中。

@questions = shuffle(@exam_group,@module)
session[:questions] = @questions.map &:id
于 2013-11-15T16:15:50.617 回答