【问题标题】:Randomize my record in ruby on rails在 ruby​​ on rails 中随机化我的记录
【发布时间】:2013-11-13 04:15:41
【问题描述】:

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

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

请帮帮我

【问题讨论】:

  • 你能解释一下“显示下一个问题的下一个功能”吗?
  • 这里我用下一个编辑

标签: mysql ruby-on-rails random shuffle


【解决方案1】:

您可以传递shuffle 一个种子,它会始终返回一个结果。您可以将此种子作为参数传递给您的 next 操作。你可以在这里阅读更多信息:Reproduce random array sort

修改您的shuffle 方法以接受一个可选的种子参数,然后您可以在您的控制器中通过一个参数传递该参数。

【讨论】:

    【解决方案2】:

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

    @questions = shuffle(@exam_group,@module)
    session[:questions] = @questions.map &:id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      相关资源
      最近更新 更多