Forum: Ruby on Rails handling AR object in multistage forms

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adam D. (Guest)
on 2006-01-12 00:10
(Received via mailing list)
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
Jeff Hoffman (Guest)
on 2006-01-12 03:33
(Received via mailing list)
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. <removed_email_address@domain.invalid>
To: removed_email_address@domain.invalid
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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
This topic is locked and can not be replied to.