views:

358

answers:

3

Hi,

Simple question - I have a form where I create an instance of an object. After I create that object (aka submit the form), I want to redirect to a new form that is associated with a different controller's action and carry that instance variable to populate some of that form's fields.

I know I typically have 2 options, store that instance variable in the session or pass it via params. I can't use sessions (for a variety of reasons I won't bore you with). The params option I am confused on.

I should know this. :( How would you go about doing this? Any examples greatly appreciated!!

Betsy

A: 

You'll have two methods on your controller. One for each form (rendered by the associated template). The first form should post to the second action. The second action can then transfer the request parameters into instance variables, to be available within the second template.

class FooController
  def bar
   # setup instance variables and render first form
  end

  def baz
    @bar_values = params[:bar]
    # setup other instance variables and render second form
  end
end

UPDATE0 Do it across two controllers using session.

class FooController
  def new_baz
    # setup instance variables and render the first form
  end

  def create_baz
    # respond to posting of form data
    session[:current_baz_values] = params
    redirect_to :action => "baq", :controller => "bar"
  end
end

class BarController
  def baq
    @baz_values = session[:current_baz_values]
    # setup other instance variables and render the second form
  end
end
Terry Lorber
This is helpful - however what happens in the case where the action (or in this case the second form) is called from a completely different controller? So in my case the action "bar" calls a show action with a form on it. When that form is submitted, it calls the "create" action and creates an instance of "bar". After it's done creating I need to redirect_to new_elephant_path, but carry that instance of @bar. I know this sounds like really bad design, but as I mull this over I am still curious about the solution for this.
Betsy
If you're going to use a redirect, then you'll need to store the parameters somewhere that will persist between requests. Use the session hash or a database table.
Terry Lorber
A: 

Could you somehow just do a find of the newly created record in the other controller, and then use that to populate the info you need?

Also, unless you are using AJAX you usually don't want to have modification actions on the show page for a record. Those belong on the edit or update page. If you always want people to be able to edit a record on the same page I would either use some AJAX on the show page, or just always return the edit/update page instead...

Shane Liebling
A: 

If you do not want to use sessions, you could use the flash variable to store your parameter. Something like, flash[:my_params] = params, and then reading it back in the next request with params = flash[:my_params]. The good thing about flash, is that persists for only the next request, and auto-clears after that.

If you are looking for passing values from the client side when using Ajax, then probably setting a hidden field with the parameters is going to pass them on to the next request.

Prem