# Multi step forms

**URL:** <https://www.ruby-forum.com/t/multi-step-forms/48193>\
**Category:** Rails\
**Created:** [November 17, 2005, 4:41pm UTC](https://www.ruby-forum.com/t/multi-step-forms/48193 "2005-11-17T16:41:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![straightflush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4bbf92/32.png) [@straightflush](https://www.ruby-forum.com/u/straightflush)\
**Post date:** [November 17, 2005, 4:41pm UTC](https://www.ruby-forum.com/t/multi-step-forms/48193/1 "2005-11-17T16:41:52Z")

</div>

Hello,

Is there an elegant way to handle forms in a controller/view that  
need to go thru multiple steps. Like 1) validating content, and 2)  
adding a step like first accepting an Agreement?

Just wondering if rails had a way to handle this without me having  
to post to the controller twice and render a view based on a hidden  
form value (input type=hidden name=step value=2 kind of thing)

thanks  
adam

---

<div class="post-metadata">

**Author:** ![straightflush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4bbf92/32.png) [@straightflush](https://www.ruby-forum.com/u/straightflush)\
**Post date:** [November 18, 2005, 1:40am UTC](https://www.ruby-forum.com/t/multi-step-forms/48193/2 "2005-11-18T01:40:26Z")

</div>

Adam D. \<straightflush@…\> writes:

> Is there an elegant way to handle forms in a controller/view that  
> need to go thru multiple steps. Like 1) validating content, and 2)  
> adding a step like first accepting an Agreement?
> 
> Just wondering if rails had a way to handle this without me having  
> to post to the controller twice and render a view based on a hidden  
> form value (input type=hidden name=step value=2 kind of thing)

I’m not sure if these are what you’re looking for, but here are three  
tricks  
I’ve used:

1. As long as you put your validation in your models, you get all the  
cool  
validation effects (like feedback on the form telling people what they  
did  
wrong, bad fields being highlighted, etc.) for free–no need to do  
separate  
pages for validation. This knocks out a lot of the things you would need  
hidden  
fields for one one fell swoop.

2. The Rails generators create CRUD scaffolding that uses two actions  
for some  
transactions that you could really do in with a single action (example:  
“new” to  
display a blank form to collect data, and “create” to accept that data  
and  
actually save it into the database). You can easily combine related  
operations  
like this into a single controller by triggering one operation if the  
action is  
called with HTTP GET, and the other if it’s called with HTTP POST. You  
write the  
action code something like this:  
def new  
if request.get?

3. Create a second form for subsidiary data (perhaps like asking for  
acceptance  
of an agreement, as you were asking about) and put it inside an HTML  
“div”  
element with inline style set to “display:none;” so that it’s invisible,  
and  
overrides it’s main CSS styling. Use Javascript to take the main form’s  
“submit”  
button, and instead of submitting, wire it to use one of the cool  
[Script.aculo.us](http://Script.aculo.us) effects in Rails to make your secondary form visible on  
top of  
the main form. You use Javascript on this secondary form’s “submit”  
button to  
make it submit the original form when clicked.

#1 and #2 are pretty straightforward and standard. #3 takes a little  
more work,  
and can be very impressive; lots of variations on that approach are  
possible. To  
see one example, take a look at the search box on the main  
[Script.aculo.us](http://Script.aculo.us) page  
([http://script.aculo.us](http://script.aculo.us)), and click on it’s “advanced options” link.

–Forrest

---

<div class="post-metadata">

**Author:** ![straightflush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4bbf92/32.png) [@straightflush](https://www.ruby-forum.com/u/straightflush)\
**Post date:** [November 18, 2005, 5:02am UTC](https://www.ruby-forum.com/t/multi-step-forms/48193/3 "2005-11-18T05:02:13Z")

</div>

thanks forrest. Step 3 actually sounds like a really interesting way  
to avoid multiple posts thru a 3 step form.

i assume the initial form’s submit button is actually not a submit  
button , but just a “button” with an onClick handler that turns the  
License Agreement div from display:none to display:block which also  
producces a submit button.

I will try this out howevr i have run into issues where combining ajax  
and forms starts preventing certain data from being submitted but i  
will definitely give this a shot. thanks for the ideas.

adam
