hi all, I am getting a problem when I am trying to remove an item from the cart using Ajax. I have a view named display_cart.rhtml, a controller store_controller.rb and a model cart.rb. I have written a method named remove_from_cart in store_controller.rb and javascript in display_cart.rhtml to display total cost after deleting the item from the cart. The method is: def remove_from_cart product = Product.find(params[:id]) @cart = find_cart @cart.remove_product(product) redirect_to :action => "display_cart" and return false end. Now the output it is showing is: Display Cart * Continue Shopping * Empty Cart * Checkout Qty Description Price Each Total 1 abc $10.00 $10.00 remove product 1 abc $10.00 $10.00 remove product Total: Total:20 Display Cart * Continue Shopping * Empty Cart * Checkout Qty Description Price Each Total 1 abc $10.00 $10.00 remove product 1 abc $10.00 $10.00 remove product 1 a $10.00 $10.00 remove product Total: $30.00. It is displaying the results as many times as I remove the items. Can anyone tell me what is the exact problem. Any help would be greatly appreciated. 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 -~----------~----~----~----~------~----~------~--~---
On 20 Nov 2007, at 05:13, Ruchita Sharma wrote:> > hi all, > > > I am getting a problem when I am trying to remove an item from the > cart > using Ajax. I have a view named display_cart.rhtml, a controller > store_controller.rb and a model cart.rb. I have written a method named > remove_from_cart in store_controller.rb and javascript in > display_cart.rhtml to display total cost after deleting the item from > the cart. > The method is: > def remove_from_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.remove_product(product) > redirect_to :action => "display_cart" and return false > end.This is probably to do with the bit you haven''t show us :-), ie whether your link_to_remote is set to update a div (which one?) and what is in display_cart.rhtml>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 05:13, Ruchita Sharma wrote: > >> the cart. >> The method is: >> def remove_from_cart >> product = Product.find(params[:id]) >> @cart = find_cart >> @cart.remove_product(product) >> redirect_to :action => "display_cart" and return false >> end. > > This is probably to do with the bit you haven''t show us :-), ie > whether your link_to_remote is set to update a div (which one?) and > what is in display_cart.rhtml >> > > Fredyeah.... link_to_remote is set to update a div named ''tablecart''. This is the method in display_cart and a div named ''tablecart'' which contains table having product name, quantity and price. <%= link_to_remote "remove product", :update => "tablecart", :url => { :action => :remove_from_cart, :controller => "store",:id => product.id }, :position =>"top", :complete => "deductprice(''#{item.unit_price}'',''#{@cart.total_price}'',''#{product.id}'');" %> Please tell me the solution. -- 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, Please do let me know the solution of above 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 -~----------~----~----~----~------~----~------~--~---
On 20 Nov 2007, at 05:31, Ruchita Sharma wrote:>> > yeah.... link_to_remote is set to update a div named ''tablecart''. This > is the method in display_cart and a div named ''tablecart'' which > contains > table having product name, quantity and price. > > > <%= link_to_remote "remove product", :update => "tablecart", :url => { > :action => :remove_from_cart, :controller => "store",:id => product.id > }, > :position =>"top", :complete => > "deductprice > (''#{item.unit_price}'',''#{@cart.total_price}'',''#{product.id}'');" > %>don''t use :position => ''top'': that means ''add the returned content to the top of the named div'', whereas what you want is to replace the content of the named div 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 05:31, Ruchita Sharma wrote: >> :position =>"top", :complete => >> "deductprice >> (''#{item.unit_price}'',''#{@cart.total_price}'',''#{product.id}'');" >> %> > > > don''t use :position => ''top'': that means ''add the returned content to > the top of the named div'', whereas what you want is to replace the > content of the named div > > FredThanks a lot Fred. This problem is removed. But one more thing. When I again click on add_to_cart... the total comes out be to be the previous total i.e total before deletion of item + the new products total. My code is: <% for item in @items 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="hidden" value="<%= 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> </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> <script language="javascript"> var total=document.getElementById(''txtTotal'').value function deductprice(ip,tot,prodid) { total = total-ip //alert (total); document.getElementById(''totalcell'').innerHTML = "Total:" + (total); document.getElementById(''tablerow[''+ prodid +'']'').style.visibility="hidden"; } </script> </body> </html> Please look into it and do let me know. Thanks a lot for your 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 -~----------~----~----~----~------~----~------~--~---