Hi all, I am implementing the functionality of shopping cart. The problem is there is a textbox for updating quantity of the product on the same page where the products are displayed after ading in the cart. By default the quantity is one and after that the user can change the quantity of the product. Its like: Qty Product Price Total textbox. abc $10.00 $10.00 remove product Total: $10.00 My problem is that I am not able to update the implement this functionality. Can anyone give me some idea of how to implement it?? Thanks, Ruchita Sharma. -- 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 -~----------~----~----~----~------~----~------~--~---
Ruchita Sharma wrote:> Hi all, > > I am implementing the functionality of shopping cart. > The problem is there is a textbox for updating quantity of the product > on the same page where the products are displayed after ading in the > cart. By default the quantity is one and after that the user can change > the quantity of the product. > > Its like: > Qty Product Price Total > > textbox. abc $10.00 $10.00 remove product > > Total: $10.00 update button for quantity. > > My problem is that I am not able to update the implement this > functionality. Can anyone give me some idea of how to implement it?? > > Thanks, > Ruchita Sharma.I have written a code like: store_controller.rb def update @quantity = request.parameters[:quantity] @product = request.parameters[:productid] quantity = @quantity #productname=@product product = @product @cart = find_cart @cart.add_product(product,quantity) render :action => ''display_cart'' end cart.rb def add_product(product,quantity) #@items << Lineitem.for_product(product) item = @items.find {|i| i.product_id == product.id } if item item.quantity += 1 else item = Lineitem.for_product(product) @items << item end But the above code is throwing error: ActiveRecord::AssociationTypeMismatch in StoreController#update Product expected, got String Can anyone tell me why it''s throwing this error??? -- 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 -~----------~----~----~----~------~----~------~--~---
On 20 Nov 2007, at 08:38, Ruchita Sharma wrote:> > But the above code is throwing error: > > ActiveRecord::AssociationTypeMismatch in StoreController#update > > Product expected, got String >presumably because @product is a string containing a product id, rather than an instance of Product. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 20 Nov 2007, at 08:38, Ruchita Sharma wrote: >> >> But the above code is throwing error: >> >> ActiveRecord::AssociationTypeMismatch in StoreController#update >> >> Product expected, got String >> > > presumably because @product is a string containing a product id, > rather than an instance of Product. > > Fredyeah I got that. Can you tell me what is the way to implement it? store_controller.rb def add_to_cart product = Product.find(params[:id]) quantity = 1 @cart = find_cart @cart.add_product(product,quantity) redirect_to(:action =>''display_cart'') rescue logger.error("Attempt to access invalid product #{params[:id]}") flash[:notice] =''Invalid product'' redirect_to(:action => ''index'') end def update @quantity = request.parameters[:quantity] @product = request.parameters[:productid] quantity = @quantity product = @product add_to_cart 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 -~----------~----~----~----~------~----~------~--~---
On 20 Nov 2007, at 09:29, Ruchita Sharma wrote:> > Frederick Cheung wrote: >> On 20 Nov 2007, at 08:38, Ruchita Sharma wrote: >>> >>> But the above code is throwing error: >>> >>> ActiveRecord::AssociationTypeMismatch in StoreController#update >>> >>> Product expected, got String >>> >> >> presumably because @product is a string containing a product id, >> rather than an instance of Product. >> >> Fred > > yeah I got that. Can you tell me what is the way to implement it? >Well you say you''ve got it, but you''re just not doing it.> def update > @quantity = request.parameters[:quantity] > @product = request.parameters[:productid]you''re just setting @product to a string rather than using Product.find Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 20 Nov 2007, at 09:29, Ruchita Sharma wrote: > >>> >>> presumably because @product is a string containing a product id, >>> rather than an instance of Product. >>> >>> Fred >> >> yeah I got that. Can you tell me what is the way to implement it? >> > Well you say you''ve got it, but you''re just not doing it. > >> def update >> @quantity = request.parameters[:quantity] >> @product = request.parameters[:productid] > > you''re just setting @product to a string rather than using Product.find > > FredHi, The code is working now but only for the first row of the table i.e only for the first product. My code is: Store_controller.rb def add_to_cart product = Product.find(params[:id]) quantity = 1 @cart = find_cart @cart.add_product(product,quantity) redirect_to(:action =>''display_cart'') rescue logger.error("Attempt to access invalid product #{params[:id]}") flash[:notice] =''Invalid product'' redirect_to(:action => ''index'') end def update @quantity = request.parameters[:quantity] product = Product.find(params[:productid]) quantity = @quantity @cart = find_cart @cart.add_product(product,quantity) redirect_to(:action =>''display_cart'') end cart.rb def add_product(product,quantity) #@items << Lineitem.for_product(product) item = @items.find {|i| i.product_id == product.id } if item item.quantity += 1 else item = Lineitem.for_product(product,quantity) @items << item end lineitem.rb def self.for_product(product,quantity) item = self.new item.quantity = quantity item.product = product item.unit_price = product.product_price item end Can you tell me wat is wrong here. Thanks a lot for the help. -- 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, Can anyone tell me what''s wrong with the above code. Th code is working but only for first product in the cart and not for other ones. -- 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 -~----------~----~----~----~------~----~------~--~---
Ruchita Sharma wrote:> Hi, > > Can anyone tell me what''s wrong with the above code. Th code is working > but only for first product in the cart and not for other ones.Hi, Please tell me the solution of this problem. -- 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 -~----------~----~----~----~------~----~------~--~---
Ruchita, I guess you might find rails irc channel a lot more useful than this mailing list, as it''s real time. Try http://www.mirc.com/ Server : irc.freenode.net Channel : #rubyonrails On Nov 20, 2007 11:54 AM, Ruchita Sharma <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ruchita Sharma wrote: > > Hi, > > > > Can anyone tell me what''s wrong with the above code. Th code is working > > but only for first product in the cart and not for other ones. > > Hi, > > Please tell me the solution of this problem. > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Cheers! - Pratik http://m.onkey.org --~--~---------~--~----~------------~-------~--~----~ 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 all, I am implementing the functionality of shopping cart. The problem is there is a textbox for updating quantity of the product on the same page where the products are displayed after adding in the cart. By default the quantity is one and after that the user can change the quantity of the product by changing the quantity in the text field and clicking the update button. The quantity is updating but only for the first product in the table. Not able to get the id of the selected product for updating the quantity. The code is as follows: <%= form_tag :controller => ''store'', :action=> ''update''%> <table cellpadding="10" cellspacing="0" id= "table"> <tr class="carttitle"> <td rowspan="2">Qty</td> <td rowspan="2">Description</td> <td colspan="2">Price</td> </tr> <tr class="carttitle"> <td>Each</td> <td>Total</td> </tr> <% i = 0 for item in @items product = item.product i = i + 1 -%> <tr> <td><input id="quantity" name = "quantity" type="text" value="<%= item.quantity %>" size="1" /></td> <td align="center"><%= text_field(''id_''+i.to_s, ''product'', ''id'') %></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 someone tell me where I am doing mistake in the above code? Thanks, Ruchita. -- 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 writing <td><%= text_field_tag "items[#{product.id}]", item.quantity, :id =>"items_#{product.id}"%></td> in my view. In controller def update params[:items].each do|id,qty| product=Product.find_by_id(id) end In params[:items] in am getting parameters like {"items"=>{"35"=>"1", "36"=>"9"}}, but when i am using this in my controller it is showing the last selected product. Can anyone tell me how to get the all parameters from params[:items].each do |id,qty|. Thanks, Ruchita Sharma. -- 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 -~----------~----~----~----~------~----~------~--~---