# Depot - empty\_cart with ajax

**URL:** <https://www.ruby-forum.com/t/depot-empty-cart-with-ajax/105459>\
**Category:** Rails\
**Created:** [May 30, 2007, 6:45am UTC](https://www.ruby-forum.com/t/depot-empty-cart-with-ajax/105459 "2007-05-30T06:45:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leandro\_Marcucci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc79bd/32.png) [@Leandro\_Marcucci](https://www.ruby-forum.com/u/Leandro_Marcucci)\
**Post date:** [May 30, 2007, 6:45am UTC](https://www.ruby-forum.com/t/depot-empty-cart-with-ajax/105459/1 "2007-05-30T06:45:21Z")

</div>

First of all id like to say hi to everyone from Argentina, im yet  
another Depot first-timer.

Going through the example, i got to the point where my cart finally  
runs Ajax-based. And there is a line where Dave says, after all the  
job is done “You should see the cart in the sidebar update. And you  
shouldn’t see your browser show any indication of reloading the page.  
You’ve just created an AJAX application.”. 10 seconds after seeing  
with my own eyes that it worked right, i tried the “empty cart”  
button, and it didnt update my sidebar. It reloaded the whole page!  
The I decided to implement the empty cart button with Ajax. Here’s  
what i did:

1. In “\_cart.rhtml” partial, I replaced the line:

\<%= button\_to “Empty cart” , :action =\> :empty\_cart %\>

with:

\<% form\_remote\_tag :url =\> { :action =\> :empty\_cart } do %\>  
\<%= submit\_tag “Empty Cart” %\>  
\<% end %\>

After that, i tried everything that “seemed” ok in order to have the  
page reload the cart without refreshing the whole page, but all i got  
was half the magic: i can add products to the cart, and i can empty  
the cart by clicking in the new button, but it wont reload the empty  
cart(after clicking empty cart, the next product added to the cart  
becomes the first and only product there, so i assume that the cart  
was empty).

After a “tail -f log/development.log” (im a linux user) i see where is  
the problem, and the origin of my question. This is the log for an  
empty cart button click:

Processing StoreController#empty\_cart (for 127.0.0.1 at 2007-05-30  
01:26:54) [POST]  
Session ID: 1ce9896c57938ff6544084ae66aea7e3  
Parameters: {“commit”=\>“Empty Cart”, “action”=\>“empty\_cart”,  
“controller”=\>“store”}  
Rendering store/empty\_cart

ActionView::TemplateError (You have a nil object when you didn’t  
expect it!  
The error occurred while evaluating nil.items) on line #8 of app/views/  
store/\_cart.rhtml:  
5: --\>  
6:

Your Cart
  
7:   
8: \<%= render(:partial =\> “cart\_item”, :collection =\> cart.items) %\>  
9:  
10:   
11: 

Then my question:

The first time the store loads, there is no “cart.items” collection,  
and it doesnt throw an exeption on nil render. How can i get my empty  
cart action to do the exact same as a first load of an empty cart?

I hope it doesnt bore everybody to death, but i just keep wondering,  
even when the future of emtpy carts is to be hidden (i jumped some  
pages forward an saw it).

In advance, thanks. Leandro.

| Total |

---

<div class="post-metadata">

**Author:** ![Leandro\_Marcucci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc79bd/32.png) [@Leandro\_Marcucci](https://www.ruby-forum.com/u/Leandro_Marcucci)\
**Post date:** [May 30, 2007, 10:52am UTC](https://www.ruby-forum.com/t/depot-empty-cart-with-ajax/105459/2 "2007-05-30T10:52:49Z")

</div>

did you find the cart in your empty\_cart\_ajax method?

@cart ||= find\_cart # or something simillar

?

you must initialize the variable, that’s why it’s not nil in the first  
case, and why it is, in yours.  
i think. just a random guess.

good luck …

–shai

Leandro Marcucci wrote:

> First of all id like to say hi to everyone from Argentina, im yet  
> another Depot first-timer.
> 
> Going through the example, i got to the point where my cart finally  
> runs Ajax-based. And there is a line where Dave says, after all the  
> job is done “You should see the cart in the sidebar update. And you  
> shouldn’t see your browser show any indication of reloading the page.  
> You’ve just created an AJAX application.”. 10 seconds after seeing  
> with my own eyes that it worked right, i tried the “empty cart”  
> button, and it didnt update my sidebar. It reloaded the whole page!  
> The I decided to implement the empty cart button with Ajax. Here’s  
> what i did:
> 
> 1. In “\_cart.rhtml” partial, I replaced the line:
> 
> \<%= button\_to “Empty cart” , :action =\> :empty\_cart %\>
> 
> with:
> 
> \<% form\_remote\_tag :url =\> { :action =\> :empty\_cart } do %\>  
> \<%= submit\_tag “Empty Cart” %\>  
> \<% end %\>
> 
> After that, i tried everything that “seemed” ok in order to have the  
> page reload the cart without refreshing the whole page, but all i got  
> was half the magic: i can add products to the cart, and i can empty  
> the cart by clicking in the new button, but it wont reload the empty  
> cart(after clicking empty cart, the next product added to the cart  
> becomes the first and only product there, so i assume that the cart  
> was empty).
> 
> After a “tail -f log/development.log” (im a linux user) i see where is  
> the problem, and the origin of my question. This is the log for an  
> empty cart button click:
> 
> Processing StoreController#empty\_cart (for 127.0.0.1 at 2007-05-30  
> 01:26:54) [POST]  
> Session ID: 1ce9896c57938ff6544084ae66aea7e3  
> Parameters: {“commit”=\>“Empty Cart”, “action”=\>“empty\_cart”,  
> “controller”=\>“store”}  
> Rendering store/empty\_cart
> 
> ActionView::TemplateError (You have a nil object when you didn’t  
> expect it!  
> The error occurred while evaluating nil.items) on line #8 of app/views/  
> store/\_cart.rhtml:  
> 5: --\>  
> 6:
> 
> Your Cart
>   
> 7:   
> 8: \<%= render(:partial =\> “cart\_item”, :collection =\> cart.items) %\>  
> 9:  
> 10:   
> 11: 
> 
> Then my question:
> 
> The first time the store loads, there is no “cart.items” collection,  
> and it doesnt throw an exeption on nil render. How can i get my empty  
> cart action to do the exact same as a first load of an empty cart?
> 
> I hope it doesnt bore everybody to death, but i just keep wondering,  
> even when the future of emtpy carts is to be hidden (i jumped some  
> pages forward an saw it).
> 
> In advance, thanks. Leandro.
> 
> | Total |

---

<div class="post-metadata">

**Author:** ![Leandro\_Marcucci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc79bd/32.png) [@Leandro\_Marcucci](https://www.ruby-forum.com/u/Leandro_Marcucci)\
**Post date:** [May 30, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/depot-empty-cart-with-ajax/105459/3 "2007-05-30T22:51:34Z")

</div>

On 30 mayo, 05:52, shai [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> did you find the cart in your empty\_cart\_ajax method?
> 
> @cart ||= find\_cart # or something simillar
> 
> ?
> 
> you must initialize the variable, that’s why it’s not nil in the first  
> case,  
> You mean that its not nill when the page loads because it doesnt  
> exist?

and why it is, in yours.

> > another Depot first-timer.
> 
> > After that, i tried everything that “seemed” ok in order to have the
> 
> > store/\_cart.rhtml:  
> > The first time the store loads, there is no “cart.items” collection,  
> > Posted viahttp://www.ruby-forum.com/.  
> > ill try next time
