Hi,
I'm creating multi page form (wizard like pages) in my application using code at
http://snippets.dzone.com/posts/show/277#related
problem ===> When I create a new coupon first time, it is stored in DB. After saving I try to clear out(delete old session in which coupon object is stored) session using session.delete('partial_coupon') But session is not get clear and also coupon object is not get store in DB.
Please tell me what went wrong?
Following is controller code----> class CouponsController < ApplicationController before_filter :get_partial_coupon_from_session, :only => [:business_coupon] after_filter :save_partial_coupon_in_session, :only => [:business_coupon]
def business_coupon case request.method when :post @current_stage = params['current_stage'] if @current_stage == "stage1" @coupon.attributes = {:title => params[:coupon][:title]} if valid_for_attributes(@coupon,["title"]) @current_stage = "stage2" end elsif @current_stage == "stage2" @coupon.attributes = {:tag_line => params[:coupon][:tag_line]} if valid_for_attributes(@coupon,["tag_line"]) @current_stage = "stage3" end elsif @current_stage == "stage3" @coupon.attributes = {:price => params[:coupon][:price]} if @coupon.save
##### HERE I TRY TO CLEAR SESSION DATA session.delete('partial_coupon') flash['notice'] = "Coupon successfully created." render :action => "show", :id => @coupon else flash[:error] = "Could not save coupon #{@coupon.errors.full_messages().join(', ')}" end
end when :get @current_stage = "stage1" end
end
private def get_partial_coupon_from_session unless session['partial_coupon'].nil? @coupon = session['partial_coupon'] else @coupon = Coupon.new end end
def save_partial_coupon_in_session unless @coupon.nil? session['partial_coupon'] = @coupon end end
def valid_for_attributes( model, attributes ) unless model.valid? errors = model.errors our_errors = Array.new errors.each { |attr,error| if attributes.include? attr our_errors << [attr,error] end } errors.clear our_errors.each { |attr,error| errors.add(attr,error) } return false unless errors.empty? end return true end
def clear_stage_errors unless session['partial_coupon'].nil? session['partial_coupon'].errors.clear end end end
############### VIEWS #############3 new.html.erb
<h1>New coupon</h1>
<% form_for(@coupon) do |f| %> <%= hidden_field_tag "current_stage", "#{@current_stage}"%> <%= f.error_messages %>
<%= render :partial => "coupons/#{@current_stage}", :locals => {:f => f} %>
<% end %> <%= link_to 'Back', coupons_path %>