1

I need to use params[:number] from the new function in the create function, how would I go about doing this?

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

EDIT: I guess I am confused as the differences between new and create then. I am taking in the parameter :number by passing it to new.

new_test_suite_run_path(:number => ts.id)

I am then using it to generate the form. I don't understand what to do in the create function then. If I remove the create function in the controller, when I submit the form in new, it gives me an error saying that there is no create action in the controller. Does that mean I have to move everything in new to the create function? How would that be possible, would I have to create a create.html.erb and move all my form information?

2
  • Seems counter-intuitive to have both a new and create function :/ seems like you would need to pass in the original :number parameter into both functions from the same place Commented Jul 10, 2012 at 1:16
  • Your use of new and create methods are unconventional. Can you tell us what you would like to accomplish? Maybe we can suggest a way of doing what you want, without doing the unconventional way. Commented Jul 10, 2012 at 1:16

2 Answers 2

5

You can use Flash: http://api.rubyonrails.org/classes/ActionDispatch/Flash.html

The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed to the very next action and then cleared out.


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
Sign up to request clarification or add additional context in comments.

Comments

2

I guess I am confused as the differences between new and create then.

Let's address this issue first.

new method generates a view for a form where Rails build a TestSuiteRun instance. This instance exists only memory temporally.

create method takes the data entered in the form, and actually saves the instance created into database permanently.

I don't think you need to change your new method.

Try changing your create method to this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.