Best way to do a wizard that creates a set of objects?

I have a multi-step user interaction to create a “parent” object that
has a few more associated with it. I need to be able to let the user
go back and forth between the pages so they can make corrections and
such.

At first, I just started creating the objects (and saving to the db)
from the first page on, but this seemed problematic in that I didnt
want to have to cleanup the objects (and the db) if the user cancelled
the process before completion.

So, I thought I’d store intermediate values in the session and then
create and associate the various objects only at the end.

I’m not sure this approach is the best because it may be a use of
session that isn’t really right and I lose the step by step validation
(though I think there are other means of validating other than what
gets performed by the callbacks… right?).

One thing I run into is that this approach needs to associate the form
fields with something and if I just ref the session variables, it
doesnt seem to work. Also, in the case that the user goes backwards in
the wizard, I need to fill the form fields with the previously entered
values.

Is there something obvious I’m missing here? Have I made this too
complicated?

lunaclaire wrote:

I’m not sure this approach is the best…

Basically, you have a choice between storing them in the session or
cleaning up a database when the user cancels the multistep operation. I
prefer to store the partially-complete objects in the session –
session[:model_name][:field_name]

…because it may be a use of
session that isn’t really right and I lose the step by step validation
(though I think there are other means of validating other than what
gets performed by the callbacks… right?).

You can make a model instance any time and assign the attributes you
have for that model in the session, something like:

@model = ModelName.new
@model.attributes = session[:model_name]

The check its validity like this –

do_stuff_for_invalid_model unless @model.valid?

Make sure your models are all valid by the time you get to the last step
of the process, then (after you make your model and assign its
attributes from the session)…

@model.save_with_validation(false)

–Al Evans

Thanks for the reply, Al.

I did decide to go with storing everything in the session and then
clearing when the user completes or cancels and I think that leaves
things in a clear state and is close enough “to the rails way”.

I didnt know about those validate methods and they may work fo rme if
I build any part of the models along the way, but I’m actually not
doing that yet until the end, so I may still be faced with the
question of how to apply validations and catch a problem while the
user is still on the interim pages because I would want them to
correct something before moving to the next page…

It’s similar to another problem I’ve faced and not figured out yet
which is how do I “validate” other forms that aren’t and wont be tied
to a model? The example is a form to send an email and I want to make
sure the addrs is valid.

Is there a general technique I’ve missed on validating forms not tied
to models?

Thx again and TIA on any further help…