RoR E-Commerce - AJAX won't fire

Trying to achieve drag and drop “add to cart” as in p. 156-160 of RoR
E-Commerce and my action works and the product is added to the cart, but
my RJS file isn’t firing and making my magic happen. What have I
missed?

class CartController < ApplicationController

def add
@book = Book.find(params[:id])

if request.xhr?
  @item = @cart.add(params[:id])
  flash.now[:cart_notice] = "#{@book.title} added to your cart."
  render :action => "add_with_ajax"
#snip!
end

add_with_ajax.rjs
page.replace_html “shopping_cart”, :partial => “cart”
page.visual_effect :highlight, “cart_item_#{@item.book.id}”,
:duration => 3
page.visual_effect :fade, “cart_notice”, :duration => 3

Things that DO work:

  1. the products are draggable (drags onto shopping_cart with method
    “add”)
  2. the drop zone “shopping_cart” works
  3. products are added to the cart, total is updated, all logic works

So if the drop zone works why isn’t page.replace_html replacing? I have
to do manual refreshes. Same with the visual_effects. This leads me to
believe that the RJS isn’t firing at all. Anyone have an idea why?

I guess I should have read the logs:

ActionView::TemplateError (undefined method `book’ for true:TrueClass)
on line #2 of app/views/cart/add_with_ajax.rjs:
1: page.replace_html “shopping_cart”, :partial => “cart”
2: page.visual_effect :highlight, “cart_item_#{@item.book.id}”,
:duration => 3
3: page.visual_effect :fade, “cart_notice”, :duration => 3

Replacing the variable in line 2 with, “cart_item_#{@book.id}” fixed it.
Thanks.

Try

render :partial => ‘add_with_ajax’

Jason

Is this an error in the book? I am compiling a list of errata to post on
my
blog. Thanks.

Taylor S. wrote:

Thanks.
Hi Taylor,

For some reason your @item object is true, instead of the actual item.
If you take a look at pages 154 and 155, you can see that @item =
@cart.add(params[:id]) and that the add method should always return a
cart item (on page 154), never a boolean value. So your fix works, but
if your code is exactly as it is in the book, you should never get the
error.

Cheers,
//jarkko


Jarkko L.

http://www.railsecommerce.com
http://odesign.fi

Bala P. wrote:

Is this an error in the book? I am compiling a list of errata to post on my
blog. Thanks.

Hi Bala,

I would appreciate if you could post all the errors you find directly
to me, we’re currently building an errata system to the companion site
@ http://www.railsecommerce.com

Thanks,
//jarkko


Jarkko L.

http://www.railsecommerce.com
http://odesign.fi