Thomas
Thomas

Reputation: 2386

rails multistep form

id like to make a multistep form with rails using the edit and update actions. so i would like it to be like step 1 of the form, and the user fills in his name, address, and phone number. then the user clicks save and continue and he then fills out his shipping address and then clicks save and continue and fills out his billing address. i saw ryan bates version, but its not what im looking for. i would like the order to be saved after the first step so if the user doesnt finish their form, i can call them and ask them what went wrong. can anyone refer me to a tutorial or give me an example of how to make an order form using the edit and update methods?

Upvotes: 3

Views: 1663

Answers (3)

The Whiz of Oz
The Whiz of Oz

Reputation: 7043

There's a great Railcast on creating multi-step wizard forms, which you can find here. It uses the Wicked gem.

Upvotes: 0

user465316
user465316

Reputation:

There are different approches to this problem.

My particular prefered solution is to implement something like a "State Machine" in the model. This way, I can persist the progress of a, per example, a multistep form, without having to hasle with more actions than new/create and edit/update.

I'm currently working on a heavy long State Machine application using the State Machine gem for rails.

Hope it helps you!

Upvotes: 0

Jack Danger
Jack Danger

Reputation: 1406

Typically this means you'll need to put conditions on your model validations. Some subset of your validations should apply to each form page:

class User
  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_presence_of :street,      :if => :on_page_two?
  validates_presence_of :city,        :if => :on_page_two?
  validates_presence_of :postal_code, :if => :on_page_two?
  validates_presence_of :state,       :if => :on_page_two?
  validates_presence_of :country,     :if => :on_page_two?
  validates_acceptance_of :terms_and_conditions, :if => :on_page_three?

  def on_page_two?
    # whatever you need to determine the page number
  end

  def on_page_three?
    # whatever you need to determine the page number
  end
end

It's not pretty but I highly recommend a pattern like this. Anything more complicated and you'll need to rewrite it when your signup flow changes.

Upvotes: 6

Related Questions