Ruby on Rails:如何在控制器函数之间传递变量?

ves*_*lll 1 ruby-on-rails

我需要在 create 函数中使用 new 函数中的 params[:number] ,我该怎么做?

def new
   @test_suite_run = TestSuiteRun.new

    @tests = Test.find(:all, :conditions => { :test_suite_id => params[:number] })
end

def create        
    @test_suite_run = TestSuiteRun.new(params[:test_suite_run])

    @tests = Test.find(:all, :conditions => { :test_suite_id => //I need the same params[:number] here})   
end
Run Code Online (Sandbox Code Playgroud)

编辑:我想我对 new 和 create 之间的差异感到困惑。我通过将参数 :number 传递给 new 来接收它。

new_test_suite_run_path(:number => ts.id)

然后我使用它来生成表单。我不明白在 create 函数中该做什么。如果我删除了控制器中的创建功能,当我在 new 中提交表单时,它会给我一个错误,说控制器中没有创建操作。这是否意味着我必须将所有内容都移到新的 create 函数中?这怎么可能,我是否必须创建一个 create.html.erb 并移动我的所有表单信息?

Onl*_*ere 5

您可以使用 Flash:http : //api.rubyonrails.org/classes/ActionDispatch/Flash.html

flash 提供了一种在动作之间传递临时对象的方法。您放置在闪光灯中的任何内容都将暴露在下一个动作中,然后被清除。


def new
   @test_suite_run = TestSuiteRun.new
   @tests = Test.find(:all, :conditions => { :test_suite_id => params[:number] })

   flash[:someval] = params[:number]
end

def create        
    @test_suite_run = TestSuiteRun.new(params[:test_suite_run])

    @tests = Test.find(:all, :conditions => { :test_suite_id => flash[:someval] })   
end
Run Code Online (Sandbox Code Playgroud)