简体   繁体   中英

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

I created a array in start for displaying the IDs one by one, and I want to the same array used in another action called next . In start I created a array called ques . I want to use ques in 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

Next:

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

The usual way to pass variables between actions is the flash variable.

In start:

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

In next:

flash[:ques]

to access the saved var.

But in your case maybe you want to save your ques in session or DB to use ques in more than these 2 actions:

In start:

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

In next:

session[:ques]

Or in DB, you maybe would need a new table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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