I was thinking of building up an activerecord object throughout the form pages and then saving it at the end, but this seems too easy (although it could be). Any ideas?
That's how I would do it, however, you will come across a couple stumbling blocks. One being validation. To solve the validation problem, you can create several validation methods, one for each page. For example, if you have an Order model which contains billing and shipping information (two pages in the controller) you can split the validation up into two methods:
You can then call these methods on each controller action. An alternative is to set the state in an instance variable and validate accordingly. This allows you to make use of the validation helper methods (such as validates_presence_of). For example:
class Order < ActiveRecord::Base
validates_presence_of :ship_address_id, :if => :shipping_state?
validates_presence_of :bill_address_id, :if => :billing_state?
@state == :shipping
@state == :billing
@state = state
You can even expand upon this and allow the model to hold an array of states so you can validate them all at the end, etc. However, rendering the appropriate page with the form fields containing the errors can be a pain, then what if multiple the validation errors span across multiple pages?
One more alternative, then I'll shut up. Skip the validation on the multipage form until the very end, then, if there are any errors, display a page containing all the fields for the model (or better yet, just the fields that need fixing). However, this doesn't work well if some fields depend upon the value of other fields.
Edit: Okay, I lied. One more variation is to split the model up into smaller models (one for each page). This allows you to do more of a RESTful design. However, the main problem with this is, if someone drops out in the middle of the process, you will have hanging models (because a model is saved through each page in the process).
Last edited by ryanb (2006-09-26 21:30:45)
- Free Ruby on Rails Screencasts