Hi all, I am implementing the shopping cart. The functionality is like I have a list of products with fields quantity, product name, unit price and total price. Quantity field is having a text box to change it. There is a update button to update the quantity of the product.Now when i update the quantity of the product it is working only for the first product of the table. It is not working for other products, because from the view i am not able to fetch the id of the product whose quantity is to be changed. My code is: <% session[:cart].items.each do |item| product = item.product -%> <tr> <td><input id="quantity" name = "quantity" type="text" value="<%item.quantity %>" size="1" /></td> <td><input id="id" name = "id" type="text" value="<%= product.id %>" size="1" /></td> <td><%= h(product.product_name) %></td> <td align="right"><%= fmt_dollars(item.unit_price) %></td> <td align="right"><%= fmt_dollars(item.unit_price * item.quantity) %></td> <td align="right"><%= link_to_remote "remove product", :update => "tablecart", :url => {:action => :remove_from_cart, :controller => "store",:id => product.id}, :complete => "deductprice(''#{item.unit_price}'',''#{@cart.total_price}'',''#{product.id}'');" %> <td align="right"> </td> </tr> <% end %> <tr> <td colspan="3" align="right"><strong>Total:</strong></td> <td id="totalcell"><%= fmt_dollars(@cart.total_price) %></td> <input type="hidden" value="<%= @cart.total_price %> " id="txtTotal"> <input type="submit" value="update"> </tr> <% form_tag %> </div> </table> Can anyone tell me where i am going wrong. Any help would be greatly appreciated. Thanks, Rohit. -- 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 -~----------~----~----~----~------~----~------~--~---
If you change this line from: <td><input id="quantity" name = "quantity" type="text" value="<%item.quantity %>" size="1" /></td> to: text_field_tag "items[#{item.id}]", item.quantity, :id => "items_#{item.id}" it will create a hash of values params[:items] that you can then cycle through and update your cart items. e.g. params[:items].each { | id, qty | ...update routine.... } HTH, Nicholas On Nov 21, 12:34 am, Rohit Mehra <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all, > > I am implementing the shopping cart. The functionality is like I have a > list of products with fields quantity, product name, unit price and > total price. Quantity field is having a text box to change it. There is > a update button to update the quantity of the product.Now when i update > the quantity of the product it is working only for the first product of > the table. It is not working for other products, because from the view i > am not able to fetch the id of the product whose quantity is to be > changed. > My code is: > > <% session[:cart].items.each do |item| > product = item.product > -%> > <tr> > <td><input id="quantity" name = "quantity" type="text" value="<%> item.quantity %>" size="1" /></td> > <td><input id="id" name = "id" type="text" value="<%= product.id %>" > size="1" /></td> > <td><%= h(product.product_name) %></td> > <td align="right"><%= fmt_dollars(item.unit_price) %></td> > <td align="right"><%= fmt_dollars(item.unit_price * item.quantity) > %></td> > <td align="right"><%= link_to_remote "remove product", :update => > "tablecart", > :url => {:action => :remove_from_cart, :controller => "store",:id => > product.id}, > :complete => > "deductprice(''#{item.unit_price}'',''...@cart.total_price}'',''#{product.id}'');" > %> > <td align="right"> > </td> > </tr> > <% end %> > <tr> > <td colspan="3" align="right"><strong>Total:</strong></td> > <td id="totalcell"><%= fmt_dollars(@cart.total_price) %></td> > > <input type="hidden" value="<%= @cart.total_price %> " id="txtTotal"> > <input type="submit" value="update"> > </tr> > <% form_tag %> > </div> > </table> > > Can anyone tell me where i am going wrong. > > Any help would be greatly appreciated. > Thanks, > Rohit. > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Nicholas Henry wrote:> If you change this line from: > > <td><input id="quantity" name = "quantity" type="text" value="<%> item.quantity %>" size="1" /></td> > > to: > > text_field_tag "items[#{item.id}]", item.quantity, :id => > "items_#{item.id}" > > it will create a hash of values params[:items] that you can then cycle > through and update your cart items. > > e.g. params[:items].each { | id, qty | ...update routine.... } > > HTH, > Nicholas > > On Nov 21, 12:34 am, Rohit Mehra <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>Hi, Thanks a lot for replying. Can you tell me further how to implement the update routine. In params[:items], i am getting the id of the item and the quantity. My update routine is: def product product = Product.find(params[:id]) @quantity = request.parameters[:qty] quantity = @quantity @cart = find_cart @cart.add_product(id,qty) redirect_to(:action =>''display_cart'') end. Can u please let me know how to implement params[:items].each { | id, qty | ...update routine.... } Thanks, Rohit. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi, I am getting the value of item id and quantity using the above method. In my controller in writing the update method as: def update params[:items].each do |id, qty| item= item.find(id) end This line item=item.find(id) gives an error. Now this is the item id i.e item in the session. If i use product = Product.find(params[:id]). It again throws an error which is obvious coz it doesn''t have a product with item id in the product table. How should I proceed further. Please do let me know. -- 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 -~----------~----~----~----~------~----~------~--~---
Rohit Mehra wrote:> Hi, > > > I am getting the value of item id and quantity using the above method. > In my controller in writing the update method as: > > def update > params[:items].each do |id, qty| > item= item.find(id) > end > This line item=item.find(id) gives an error. > Now this is the item id i.e item in the session. If i use product = > Product.find(params[:id]). It again throws an error which is obvious coz > it doesn''t have a product with item id in the product table. > How should I proceed further. > > Please do let me know.Hi, View is: text_field_tag "items[#{product.id}]", item.quantity, :id => "items_#{product.id}" My update method is: params[:items].each do|id,qty| product = Product.find_by_id(id) But since item is a session, everytime product is fetching last value of the session. cab anyone tell me what needs to be done??? -- 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 -~----------~----~----~----~------~----~------~--~---