small medium large xlarge


ys says:

what i did was

1. add format.js in carts controller destroy method

2. add remote: true on empty button

3. create destroy.js.erb in views/carts with

$('#cart').html("<%=j render @cart %>").hide('blind', 1000);

it works fine but seems not following given hint…

suke says:

Does the cart appear when you access the page at first?


Does the cart appear after you empty the cart and reload the page?

michael says:

@ys: in your 3rd step i did this:

$('#cart tr').not('.total_line').remove();
$('#cart').hide('blind', 1000);

jack says:

@michael: why do we need to use the .not. I had used only the #cart.hide but that did not work after emptying the cart and then adding to it again. However, your solution works great. Can you explain this a little more please?

Alex says:

@jack: The reason you use the not() is to remove all tr entires from the table, except for #total_line. The reason you want to do this is because in create.js.erb we told the browser to show() the cart if, and only if the number of tr entires under #cart is 1.

In other words, when you click Add to Cart button, the very first thing that happens is the JavaScript checking to see how many tr elements available (length). If the length == 1, we call show(); otherwise we don’t. If we were to remove total_line, the result, after removal, of length would be 0*.

The reason your method doesn’t work is because after clearing, length equals 0. When you then push Add to Cart, the *show() is skipped, and we render @cart. What show() does is essentially revert what the changes we did with hidden_div_if() helper, setting display: none. Since we did no re-fetch application.html.erb, but rather only ask to fetch _cart.html.erb, via the 2nd line in create.js.erb, the logic of hidden_div_if() is never run on the server, and we never run it on the browser either, because length = 0 after we removed total_line.

TL;DR you need total_line so that JQuery’s show() will execute, removing display:none which was set via Empty Cart.

Page History
  • V43: Juan Carlos Pazmino [18 days ago]
  • V42: Juan Carlos Pazmino [18 days ago]
  • V41: Michael Guren [over 3 years ago]
  • V39: Ian McNaney [over 3 years ago]
  • V40: System [over 3 years ago]
  • V41: Malcolm McKeown [almost 4 years ago]
  • V40: Malcolm McKeown [almost 4 years ago]
  • V39: Malcolm McKeown [almost 4 years ago]
  • V38: Malcolm McKeown [almost 4 years ago]
  • V37: Malcolm McKeown [almost 4 years ago]