简体   繁体   English

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

[英]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 . 我在start创建了一个数组,用于逐个显示ID,并且我希望在另一个名为next操作中使用相同的数组。 In start I created a array called ques . start我创建了一个名为ques的数组。 I want to use ques in next . 我要在next使用ques

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. 在动作之间传递变量的常用方法是flash变量。

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: 但是,你的情况可能要保存在会话或DB你的疑问句用ques比这2个动作的更多:

In start: 开始时:

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

In next: 在接下来的:

session[:ques]

Or in DB, you maybe would need a new table. 或者在DB中,您可能需要一个新表。

暂无
暂无

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

相关问题 在Ruby on Rails中,如何将控制权传递给另一个控制器中的操作? - In Ruby on Rails, how do I pass control to an action in another controller? Rails-5:如何将数组(has_many关联)传递给控制器​​操作 - Rails - 5: How do I pass an array (has_many association) to the controller action 如何将控制器和动作参数传递给 Rails 3 中的 javascript 函数? - How do I pass the controller and action parameters to a javascript function in Rails 3? 如何将动作转发到Rails中的另一个控制器? - How do I forward an action to another controller in Rails? 如何创建另一个控制器动作以在Rails中创建对象? - How do I create another controller action to create an object in rails? Rails - 如何避免必须从另一个控制器中的create action触发一个控制器中的create操作 - Rails - how do I avoid having to trigger the create action in one controller from the create action in another controller 如何从控制器动作中调用另一个控制器动作? - How do I call another controller action from within a controller action? 如何通过导轨将对象从一个控制器传递到另一个控制器? - How do I pass objects from one controller to another in rails? 如何在我指定控制器和操作的link_to调用中传递多个参数? - How do I pass more than one parameter into a link_to call where I specify controller and action? Rails:如何在同一个控制器中将值从一个动作传递到另一个动作 - Rails: How to pass value from one action to another in the same controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM