繁体   English   中英

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

[英]How do I pass an array to another action in a controller?

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

开始:

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

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

开始时:

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

在接下来的:

flash[:ques]

访问保存的变量。

但是,你的情况可能要保存在会话或DB你的疑问句用ques比这2个动作的更多:

开始时:

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

在接下来的:

session[:ques]

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM