small medium large xlarge



I’ll just leave my notes here:

The button_to that worked for me: <%= button_to ‘Remove one’, decrement_line_item_path(line_item), remote: true %>

In my decrement method, I had if @line_item.update_attributes(params[:id]) which contained respond_to. Turns out this didn’t work if the line_item should disappear because if you run @line_item.destroy BEFORE update_attributes, you get a “frozen hash” error that not only returns false but returns a runtime error. I got around this with the following:

if @line_item.quantity == 0
begin @line_item.update_attributes(params[:line_item])
respond_to do |format|
  format.js {@current_item = @line_item}

Also, turns out that the book uses a little hack to make the jquery blind methods go. I had to use the following to get the cart to disappear when the last item had been decremented out of existence:

$(’#cart’).html(”<%= j render @cart %>”); if ($(’#cart tr’).length 1) { $('#cart').hide('blind', 1000); } $('#current_item').css({'background-color':'#88ff88'}).animate({'background-color':'#114411'}, 1300);

When I had if ($('#cart tr').length 0, it would never work because the cart grand total is inside a tr that never goes away. This wasn’t made obvious when we were building our create.js.erb in the example so it took me a bit to figure out.

I am a little glad I learned so much, but mostly just angry right now.

Christian says:

Hi there, could you do me a big favour and share the complete code? I’ve been fiddling around with this task for hours but I just can’t get it to work (the ajax part).

Page History
  • V41: Mark Coleman [11 months ago]
  • V40: Mark Coleman [11 months ago]
  • V39: Mark Coleman [11 months ago]
  • V38: Mark Coleman [11 months ago]
  • V37: Mark Coleman [11 months ago]
  • V36: Mark Coleman [11 months ago]
  • V35: John Nguyen [about 1 year ago]
  • V34: John Nguyen [about 1 year ago]
  • V33: John Nguyen [about 1 year ago]
  • V32: Pit Verras [over 2 years ago]