Remove from cart functionality

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.

On 20 Nov 2007, at 05:13, Ruchita S. 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

Fred

Frederick C. wrote:

On 20 Nov 2007, at 05:13, Ruchita S. 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

Fred
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}’);”
%>

Please tell me the solution.

Hi,

Please do let me know the solution of above problem.

On 20 Nov 2007, at 05:31, Ruchita S. 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

Fred

Frederick C. wrote:

On 20 Nov 2007, at 05:31, Ruchita S. 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

Fred

Thanks 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
-%>

<%= h(product.product_name) %> <%= fmt_dollars(item.unit_price) %> <%= fmt_dollars(item.unit_price * item.quantity) %> <%= 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}');" %> <% end %> Total: <%= fmt_dollars(@cart.total_price) %> <% form_tag %>

Please look into it and do let me know.
Thanks a lot for your help.