# Remove from cart functionality

**URL:** <https://www.ruby-forum.com/t/remove-from-cart-functionality/120730>\
**Category:** Rails\
**Created:** [November 20, 2007, 6:13am UTC](https://www.ruby-forum.com/t/remove-from-cart-functionality/120730 "2007-11-20T06:13:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 6:13am UTC](https://www.ruby-forum.com/t/remove-from-cart-functionality/120730/1 "2007-11-20T06:13:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 6:22am UTC](https://www.ruby-forum.com/t/remove-from-cart-functionality/120730/2 "2007-11-20T06:22:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 6:31am UTC](https://www.ruby-forum.com/t/remove-from-cart-functionality/120730/3 "2007-11-20T06:31:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 7:21am UTC](https://www.ruby-forum.com/t/remove-from-cart-functionality/120730/4 "2007-11-20T07:21:00Z")

</div>

Hi,

Please do let me know the solution of above problem.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 12:48pm UTC](https://www.ruby-forum.com/t/remove-from-cart-functionality/120730/5 "2007-11-20T12:48:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [November 20, 2007, 12:59pm UTC](https://www.ruby-forum.com/t/remove-from-cart-functionality/120730/6 "2007-11-20T12:59:49Z")

</div>

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.
