如何将数组传递给控制器​​中的另一个操作?

时间:2013-11-19 01:31:13

标签: ruby-on-rails ruby arrays

我在start中创建了一个数组,用于逐个显示ID,我希望在另一个名为next的操作中使用相同的数组。在start中,我创建了一个名为ques的数组。我想在ques中使用next

START:

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 = shuf_order(@exam_group,@module)
    ques = []
    @questions.each do |a|
      ques.push q.id unless q.id.nil?
    end
    a = ques.first
    @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]
  @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
  @questions = shuf_order(@exam_group,student_additional_field_id)
  a = @questions.first
  @answer.modules_id = student_additional_field_id
  if @answer.save
    @ans = Question.find_by_id(a, :conditions => [' id not in (?)',answered])
    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

1 个答案:

答案 0 :(得分:0)

在操作之间传递变量的常用方法是flash变量。

开始时:

flash[:ques] = []
flash[:ques].push 'whatever'

接下来:

flash[:ques]

访问已保存的变种

但是在您的情况下,您可能希望在会话或数据库中保存您的问题,以便在以下两个操作中使用ques

开始时:

session[:ques] = []
session[:ques].push 'whatever'

接下来:

session[:ques]

或者在DB中,您可能需要一个新表。