how to create wizard forms in ruby on rails
Asked Answered
L

5

18

I'm trying to understand the best options for pulling off a wizard form in ruby on rails. Ideally I'd like to have it so the application signup has a back and next button that allows the user to submit data in steps.

So in step 1 they could fill out contact info. Once they are done they could click next and be on step 2 to fill out payment info, etc. If they make a mistake, they can click back and correct it. Some steps will be required, while others will not, but you do have to make it to the last step to submit the data to the database to sign up. They then need the ability to go back and fill out the past steps in the same fashion after completion. (example: perhaps if they clicked on a profile link they could recomplete the steps in the same fashion because they didn't want to complete all the steps right away. Maybe by being given a skip button before they completed the steps to sign up?). I also need validation to happen on what steps have been completed preventing them from moving onto the next step until corrected or completed.

Option 1) I've noticed that ajax has been recommended as an option in other questions on stackoverflow. The only problem I have with it is that the user would not be able to sign up if javascript was disabled. Ideally I'd like to have it be native to ruby on rails but I'm willing to work with whatever is necessary to get it to work.

Loosen answered 8/4, 2010 at 21:6 Comment(0)
U
15

There are a couple of plugins that provide wizzard construction facilitation in rails.
Acts as Wizard and Wizardly seem the most popular.
The main idea is to:
* create a model in the first step
* then edit it on subsequent steps,
* applying partial validation on each step and
* having the model implement some sort of state machine.

Ureide answered 8/4, 2010 at 21:12 Comment(7)
Thanks, I'll definitely give these a shot.Loosen
This is a good method (especially when dealing with file uploads), but neither of those gems work with Rails 3. Are there any others available?Pothouse
I don't know, maybe try to port one, it should not be hard, or make your own, the technique is a pretty low hanging fruit.Ureide
I'm getting the impression the gems haven't been moved over to rails 3 since jQuery can accomplish the same thingBarros
why model should ever be aware of which way is it filled (I mean built-in FSM)?Reptilian
@develop7 you could separate concerns, but since validation is at model layer you would probably work twice as much with little to none benefitUreide
@clyfe: ya, looks like 'only place for validation is model' principle is root of all related evil.Reptilian
B
16

You should watch this rails cast episode on multi step forms:

http://railscasts.com/episodes/217-multistep-forms

Be prepared to rewind and watch again. He's very quick!

Bailment answered 19/11, 2010 at 10:7 Comment(1)
Thanks for the recommendation. I'm actually a frequent watcher of Ryan's casts and ran across this on my own as well. Thanks anyways.Loosen
U
15

There are a couple of plugins that provide wizzard construction facilitation in rails.
Acts as Wizard and Wizardly seem the most popular.
The main idea is to:
* create a model in the first step
* then edit it on subsequent steps,
* applying partial validation on each step and
* having the model implement some sort of state machine.

Ureide answered 8/4, 2010 at 21:12 Comment(7)
Thanks, I'll definitely give these a shot.Loosen
This is a good method (especially when dealing with file uploads), but neither of those gems work with Rails 3. Are there any others available?Pothouse
I don't know, maybe try to port one, it should not be hard, or make your own, the technique is a pretty low hanging fruit.Ureide
I'm getting the impression the gems haven't been moved over to rails 3 since jQuery can accomplish the same thingBarros
why model should ever be aware of which way is it filled (I mean built-in FSM)?Reptilian
@develop7 you could separate concerns, but since validation is at model layer you would probably work twice as much with little to none benefitUreide
@clyfe: ya, looks like 'only place for validation is model' principle is root of all related evil.Reptilian
W
6

Wicked looks promising for rails 3:

https://github.com/schneems/wicked

Whimsey answered 12/6, 2012 at 17:58 Comment(0)
E
5

Have a look at wicked, explained in this rails cast: http://railscasts.com/episodes/346-wizard-forms-with-wicked?autoplay=true

Evensong answered 7/2, 2013 at 20:37 Comment(0)
M
3

An alternative approach, for simpler multi step forms especially, is to simply show-hide parts of a single form depending on the step, this way you don't hit the database on every step but rather let the user build up his object until he's ready with a valid instance.

Such an approach strongly favors using a form class instead of working with the model directly (http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/) and you need to tweak error message rendering a bit.

Pros: only one db hit, no hassle with persisting invalid instances (not null columns, before_save sanity checks for messed up attributes), no callback hell

Cons: more html sent to user, error message tweaks, requires a well built form class to be elegant and really useful

Mita answered 2/4, 2013 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.