# Handling 2 forms in one controller

**URL:** <https://www.ruby-forum.com/t/handling-2-forms-in-one-controller/70350>\
**Category:** Rails\
**Created:** [September 28, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/handling-2-forms-in-one-controller/70350 "2006-09-28T15:31:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [September 28, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/handling-2-forms-in-one-controller/70350/1 "2006-09-28T15:31:19Z")

</div>

Hi,

a brief description of my situation: I have 2 models, Item and  
SellerContactInfo. They have to be filled with data from the user, which  
is done in a 2-step web form. I’m thinking heavily about the optimal way  
to do this:

A) Create 2 actions inside the same controller, use the session to store  
the Item object and persist both objects to the db after the 2nd form  
has been completed,

or

B) Create both forms on one HTML page, switch between them by  
showing/hiding DIVs. Problem in this case: if the user fills out the 2nd  
form and then validation fails for the 1st form, he/she will be thrown  
back at the first form which reduces usability in my opinion.

Anyone have a usable idea of how to optimally solve this situation?

thx,  
Robert

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [September 28, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/handling-2-forms-in-one-controller/70350/2 "2006-09-28T17:24:24Z")

</div>

C) Create one action. Use request.post? To see if they’re just arriving  
or have posted the form. Then use another if statement based on the  
submit button’s value to see which form part has been submitted. If it’s  
the first part, you can do your validation, store information in the  
session and display the second part of the form. If the second form has  
been submitted, you can do that validation and store your data.

Would that work?

Jason

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [September 28, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/handling-2-forms-in-one-controller/70350/3 "2006-09-28T17:36:29Z")

</div>

On 28 September 2006 17:31, Robert S. wrote:

> B) Create both forms on one HTML page, switch between them by  
> showing/hiding DIVs. Problem in this case: if the user fills out the 2nd  
> form and then validation fails for the 1st form, he/she will be thrown  
> back at the first form which reduces usability in my opinion.  
> I wouldn’t go with showing/hiding DIVs… It’s too problematic for me.

You can just make 3 actions (2 actions - for two forms and one to store  
data  
in DB and redirect), submit first form to second action, and second form

- to  
third. When validation of first form in second action fails, just  
display  
first template (the same with second form). All submitted data could be  
stored in hidden fields so you don’t need to mess up with sessions.

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [September 28, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/handling-2-forms-in-one-controller/70350/4 "2006-09-28T17:32:53Z")

</div>

I’ll try, thanks for sharing the idea 🙂
