1

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

編集:私は、新しいものと作成したものの違いとして混乱していると思います。パラメータ :number を new に渡すことで取り込んでいます。

new_test_suite_run_path(:number => ts.id)

次に、それを使用してフォームを生成しています。その場合、作成機能で何をすべきかわかりません。コントローラーの create 関数を削除すると、フォームを new で送信すると、コントローラーに create アクションがないというエラーが表示されます。それは、すべてを new で create 関数に移動する必要があるということですか? create.html.erb を作成し、すべてのフォーム情報を移動する必要がありますか?

4

2 に答える 2

5

Flash を使用できます: http://api.rubyonrails.org/classes/ActionDispatch/Flash.html

フラッシュは、アクション間で一時オブジェクトを渡す方法を提供します。フラッシュに配置したものはすべて、次のアクションにさらされてから消去されます。


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
于 2012-07-10T01:15:34.100 に答える
2

new と create then の違いに戸惑っていると思います。

まずこの問題に取り組みましょう。

newメソッドは、Rails が TestSuiteRun インスタンスを構築するフォームのビューを生成します。このインスタンスは一時的にメモリのみ存在します。

createメソッドは、フォームに入力されたデータを取得し、実際に作成されたインスタンスをデータベースに永続的に保存します。

やり方を変える必要はないと思いますnew

createこれに方法を変更してみてください。

def create
  @test_suite_run = TestSuiteRun.new(params[:test_suite_run])
  @test_suite_run.save
end
于 2012-07-10T01:51:11.820 に答える