Multipage Forms

I think this may actually be an object oriented programming question.
Not only am I new to Ruby and Rails, but I’m also coming from a
functional PHP background, so this whole OO approach to web programming
is new to me.

I have a three page registration form and I am trying to determine best
practices for managing the data across forms in a session. I would
like each form to validate before passing the user along to the next
page. The data will be ultimately be distributed across 6 different
tables, so the validation criteria for the registration form is spread
accross the 6 repective model objects.

My thought is to create a non-ActiveRecord model called “Registration”
and have it create the various model objects as instance variables upon
instantiation:

class Registration
def initialize
@member = Member.new
@membership = Membership.new
@donations = Donation.new

end
end

and then save this in this object in the session. When I need to
validate the form data, I could do something like this in the
registration controller:

@registration = session[:registration] ||= Registration.new
@member = @registration.member
@member.attributes = params[:member]
redirect_to :action => ‘next_page’ if @member.valid?

That way, I can still use the model (and ActiveRecord) to handle the
nitty gritty of form validation and I only need to manage one object in
the session. Also, if I have a form with data that belongs in three
seperate models how do I validate that data across the various models?
My guess is to add something like this in my registration model:

def valid?
@member.valid?
@membership.valid?
@donation.valid?
@errors =
@member.errors.merge(@membership.errors).merge(@donation.errors)
end

And then this in the view

<%= error_messages_for ‘registration’ %>

Does that look right? I’d like to know if these are common best
practices for handling multipage forms in Rails and if not, how do the
rest of you do it?

Thanks,
Greg

There is one very important issue to keep in mind here: ActiveRecord
models
aren’t fully serializable. This means that you can’t create a model and
throw it into the session without saving, as you will lose the data and
the
app will probably crash. I recently had to deal with this same situation
(model information over like 6 pages) and it took us a while to figure
out a
good solution. Unfortunately the best solution was keeping our own hash
in
the session and only when we are done collecting all of the data do we
pull
those values into a new model for validation and saving.

If you are ok saving a partially complete AR object, then you’re fine
using
the session (for note, only the ID is guarenteed to be saved, AR repolls
from the database when needed).

Hope that helps. Just remember that even though placing unsaved AR
objects
in the session may be working for you, there is no guarentee that it
will
work anywhere else.

Jason

I try to avoid using the session as much as possible for these. Your
forms
are bound to models with validation, so just let them do the work for
you.
You can do all sorts of cool things to avoid validating fields until you
need to.

I don’t think there’s any reason to make your own model that you’ll
store in
session just to wrap existing models. Use them as they are and save the
records. If, for some reason, you don’t want to save the record
(incomplete
transaction, etc) you could just store the object directly to the
session
and only save it upon completion.

However, for a shopping cart, there’s actually some value in saving as
they
go becuase you’d have some data you could look at to help find out why
people left your store in the middle of a transaction. Just add an
additional status field to your table… 0 for default, 1 for submitted,
2
for in process, 3 for fulfillment, 4 for complete, 5 for cancelled… etc

This is just off the top of my head though. There are lots of ways to do
this.

The Great Mochini wrote:

I think this may actually be an object oriented programming question.
Not only am I new to Ruby and Rails, but I’m also coming from a
functional PHP background, so this whole OO approach to web programming
is new to me.

I have a three page registration form and I am trying to determine best
practices for managing the data across forms in a session. I would
like each form to validate before passing the user along to the next
page. The data will be ultimately be distributed across 6 different
tables, so the validation criteria for the registration form is spread
accross the 6 repective model objects.

My thought is to create a non-ActiveRecord model called “Registration”
and have it create the various model objects as instance variables upon
instantiation:

class Registration
def initialize
@member = Member.new
@membership = Membership.new
@donations = Donation.new

end
end

and then save this in this object in the session. When I need to
validate the form data, I could do something like this in the
registration controller:

@registration = session[:registration] ||= Registration.new
@member = @registration.member
@member.attributes = params[:member]
redirect_to :action => ‘next_page’ if @member.valid?

That way, I can still use the model (and ActiveRecord) to handle the
nitty gritty of form validation and I only need to manage one object in
the session. Also, if I have a form with data that belongs in three
seperate models how do I validate that data across the various models?
My guess is to add something like this in my registration model:

def valid?
@member.valid?
@membership.valid?
@donation.valid?
@errors =
@member.errors.merge(@membership.errors).merge(@donation.errors)
end

And then this in the view

<%= error_messages_for ‘registration’ %>

Does that look right? I’d like to know if these are common best
practices for handling multipage forms in Rails and if not, how do the
rest of you do it?

Thanks,
Greg

Greg, have you tried this? If so, what were your results?

I’m struggling with the same concept here – coming from a coldfusion
background. It’s not difficult to save various objects (or one master
object in your case) to the session and manipulate that data, but it’s
considerably more difficult to get the framework validation working on
multiple models accross multiple forms and properly display the errors,
and properly handle the form redirects/resubmits as necessary. I’m
amazed that there aren’t tons of examples of this sort of thing floating
around – it’s such a basic web application thing, but so far I’ve found
almost nothing on it. One link that was somewhat helpful is here:
http://www.nabble.com/Validating-two-models-from-one-form-t1421132.html

I’m looking at finishing a simple shopping cart checkout, with forms in
this order: Customer info → credit card info (with payment amount in
this form). My db tables/models are “order” (contains customer info),
“credit_card” and “payment”, one-to-many between each going left to
right.

First I save the customer info into the session (session[:order]), and
proceed to the credit_card / payment amount form. When that is
submitted, I pull the order (customer info) out of the session, push the
payment into the credit card (@credit_card.payments << @payment), push
the credit card into the order (@order.credit_cards << @credit_card),
and save the @order. The save works fine, but getting it to validate
properly has been a nightmare.

Anyway, I’m very curious to see responses to this post as I think for
seasoned web deveopers moving to RoR, this must be a very common
issue. Come on, Rails gurus! help us out! Surely there are some common
practices used in Rails for this sort of thing??

-KG