Consecutive post? if statments form(s) processing error

In the flow of my application, the user is taken from one page where
they register to another page containing a second form where they submit
more information for their contact record. I am having two problems:

  1. The second page loads because its method is called and render is
    called on the method in the controller after the record started in the
    form is created:

def method 1
#…instance variable declarations…#
if request.post? and @user.save
flash.now[:notice] = “success”
method2
render :action => ‘method2’
end
end
def method 2
#…instance variable declarations…#
if request.post? and @address.save
@user.add_address(@address)
flash.now[:notice] = “success”
method3
render :action => ‘method3’
end
end

The problem is that method 2 is called as part of a post request so the
request.post? check does not work to prevent this code block from being
hit until the form is submitted. In other words, the if statement in
method 2 is entered on the first go around. This problem I was able to
fix by checking the params[:commit] because it is “submit” for method 1
and “Ok” for method 2. However, this does not fix the second problem,
which has me stumped…

  1. If method2 is rendered from method1’s post call after saving @user
    rather than from a get get request performed directly in the browser’s
    address bar, then when form2 on mothod2’s view is submitted, the system
    calls the post request from method1, not method2. It is like the post
    request is stuck in the server. In other words, because the view
    associated with method2 is called via a post request from method 1
    rather than a get, when “Ok” is clicked on form2, it performs the same
    post request as previously: “Submit” calling on method1. However, if I
    use the address line to call the method directly, i.e.
    …/controller/method2, the form works properly.

How do I address this?

Thank you, Sam

Sam W. wrote:

In the flow of my application, the user is taken from one page where
they register to another page containing a second form where they submit
more information for their contact record. I am having two problems:

It might be better if you separate your actions in to GET and POST
operations.
Then the sequence of flow may look like:

/method1 - display form 1
/method1_post - save @user and redirect_to /method2
/method2 - display form 2
/method2_post - save @address and redirect_to /method3
/method3 - display whatever

Validation can occur in _post actions and redirect :back if validation
fails.

HTH,

Long
www.edgesoft.ca/blog/read/2