Pt-F-2-31
I’ll just leave my notes here:
The button_to that worked for me:
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
@line_item.destroy
end
begin @line_item.update_attributes(params[:line_item])
rescue
end
respond_to do |format|
format.js {@current_item = @line_item}
end
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.

