Depot - empty_cart with ajax

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: <div class="cart-title">Your Cart</div> 7: <table> 8: <%= render(:partial => "cart_item", :collection => cart.items) %> 9: 10: <tr class="total-line"> 11: <td colspan="2">Total</td>

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.

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.

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: <div class="cart-title">Your Cart</div> > 7: <table> > 8: <%= render(:partial => "cart_item", :collection => cart.items) %> > 9: > 10: <tr class="total-line"> > 11: <td colspan="2">Total</td>

> 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.

-- Posted viahttp://www.ruby-forum.com/.

ill try next time