Guo Yangguang
2008-Feb-06 11:06 UTC
how to use form to update the model stored in the session
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Guo Yangguang
2008-Feb-06 17:17 UTC
Re: how to use form to update the model stored in the session
who can help me? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stephan Wehner
2008-Feb-06 17:32 UTC
Re: how to use form to update the model stored in the sessio
Guo Yangguang 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Guo Yangguang
2008-Feb-06 18:23 UTC
Re: how to use form to update the model stored in the sessio
Thank you for reminding me ! Stephan Wehner wrote:> Guo Yangguang 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---