# How to use form to update the model stored in the session

**URL:** <https://www.ruby-forum.com/t/how-to-use-form-to-update-the-model-stored-in-the-session/128283>\
**Category:** Rails\
**Created:** [February 6, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/how-to-use-form-to-update-the-model-stored-in-the-session/128283 "2008-02-06T12:06:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [February 6, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/how-to-use-form-to-update-the-model-stored-in-the-session/128283/1 "2008-02-06T12:06:23Z")

</div>

hello:  
I am building a shopping\_cart.Before the user proceed to check out,i  
want the user to be able to modify the quantity for any cart\_item,so  
there is a quantity update function which is a rails form in the cart  
page.

1. cart information are kept in the session,cart are made of some  
cart\_items,every cart\_item has title,price,quantity…attributes.
2. cart\_item is not stored in the database,but there is cart\_item  
model,every cart\_item object are made from the product object which has  
corresponding model and table in the database.  
My problem: when i use form to update the quantity attributes for  
some cart\_item,the corresponding method can not output the expected  
result, the quantity does not changed.Who can help me?The following are  
my codes:  
\<%=form\_tag(:action=\>“quantity\_update”,:id=\>cart\_item)-%\>  
\<%=text\_field :cart\_item,:quantity,“size” =\> 10 -%\>  
\<%=submit\_tag “recalculate”%\>  
\<%=end\_form\_tag-%\>  
def quantity\_update  
@cart=find\_cart  
#items is an array storing the cart\_items  
modified\_item=@cart.items.find{|i|i==params[:id]}  
if modified\_item  
index=@cart.items.index(modified\_item)  
modified\_item.quantity=params[:cart\_item][:quantity]  
@cart.items[index]=modified\_item  
return @cart  
end  
end  
private  
def find\_cart  
session[:cart]||=Cart.new  
end

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [February 6, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/how-to-use-form-to-update-the-model-stored-in-the-session/128283/2 "2008-02-06T18:17:42Z")

</div>

who can help me?

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [February 6, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/how-to-use-form-to-update-the-model-stored-in-the-session/128283/3 "2008-02-06T18:32:05Z")

</div>

Guo Y. wrote:

> who can help me?

Could it be that in

modified\_item=@cart.items.find{|i|i==params[:id]}

the i is an integer, whereas params[:id] is a string.

A simple guess.

Stephan

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [February 6, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/how-to-use-form-to-update-the-model-stored-in-the-session/128283/4 "2008-02-06T19:23:01Z")

</div>

Thank you for reminding me !

Stephan W. wrote:

> Guo Y. wrote:
> 
> > who can help me?
> 
> Could it be that in
> 
> modified\_item=@cart.items.find{|i|i==params[:id]}
> 
> the i is an integer, whereas params[:id] is a string.
> 
> A simple guess.
> 
> Stephan
