Handling AR object in multistage forms

in a multi-stage form, what is the proper way for handling the passing
of the object thru each step ?

If i have a database called events, and my controller looks like

step = @params[:step] || 1

if step == 1
@event = Event.new
@event = @session[:event] if @session[:event]

elsif step == 2
@event = Event.new
@event = @params[:event]

elsif step == 3
@event = Event.new
@event = @params[:event]
@session[:event] = @event

if @event.save
flash[:notice] = ‘Event saved’
@session[:event] = nil
redirect_to => :index
else
render(“step1”)
end

end

This doesnt seem right, any guidance would be appreciated.

thanks
adam

Try something like this:

class YourController < ApplicationController
before_filter :normalize_session

def normalize_session
@event = session[:event] ||= Event.new
end

def process
step = params[:step] || 1
@event.attributes = params[:event] if params.has_key? :event

if step < 3
  # render step
else
  if @event.save
    flash[:notice] = 'Event saved'
    session[:event] = nil
    redirect_to :action => :index
  end
end

end
end

You may alse consider breaking each step into its own action, and
chaining them with logic in your controller.

Regards,

Jeff

----- Original Message ----
From: Adam D. [email protected]
To: [email protected]
Sent: Wed Jan 11 17:08:36 2006
Subject: [Rails] handling AR object in multistage forms

in a multi-stage form, what is the proper way for handling the passing
of the object thru each step ?

If i have a database called events, and my controller looks like

step = @params[:step] || 1

if step == 1
@event = Event.new
@event = @session[:event] if @session[:event]

elsif step == 2
@event = Event.new
@event = @params[:event]

elsif step == 3
@event = Event.new
@event = @params[:event]
@session[:event] = @event

if @event.save
flash[:notice] = ‘Event saved’
@session[:event] = nil
redirect_to => :index
else
render(“step1”)
end

end

This doesnt seem right, any guidance would be appreciated.

thanks
adam


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails