# Record is not store in DB & session.delete('key') not workin

**URL:** <https://www.ruby-forum.com/t/record-is-not-store-in-db-session-delete-key-not-workin/172239>\
**Category:** Rails\
**Created:** [July 17, 2009, 7:47am UTC](https://www.ruby-forum.com/t/record-is-not-store-in-db-session-delete-key-not-workin/172239 "2009-07-17T07:47:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [July 17, 2009, 7:47am UTC](https://www.ruby-forum.com/t/record-is-not-store-in-db-session-delete-key-not-workin/172239/1 "2009-07-17T07:47:02Z")

</div>

Hi,

I’m creating multi page form (wizard like pages) in my application using  
code at

[http://snippets.dzone.com/posts/show/277#related](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

# New coupon

\<% 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 %\>

* * *

\_stage1.html.erb

Title  
 \<%= text\_field "coupon","title" %\>

\<%= submit\_tag 'Next' %\>

* * *

\_stage2.html.erb

Tag line  
 \<%= text\_field "coupon","tag\_line" %\>

\<%= submit\_tag 'Next' %\>

* * *

\_stage3.html.erb

Price  
 \<%= text\_field "coupon", "price" %\>

\<%= submit\_tag 'Create' %\>

Please tell me where went wrong.?

Thanks,  
Vikas
