# RoR E-Commerce - AJAX won't fire

**URL:** <https://www.ruby-forum.com/t/ror-e-commerce-ajax-wont-fire/78610>\
**Category:** Rails\
**Created:** [December 7, 2006, 8:00am UTC](https://www.ruby-forum.com/t/ror-e-commerce-ajax-wont-fire/78610 "2006-12-07T08:00:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 7, 2006, 8:00am UTC](https://www.ruby-forum.com/t/ror-e-commerce-ajax-wont-fire/78610/1 "2006-12-07T08:00:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 7, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/ror-e-commerce-ajax-wont-fire/78610/2 "2006-12-07T18:17:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 7, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/ror-e-commerce-ajax-wont-fire/78610/3 "2006-12-07T16:19:13Z")

</div>

Try

render :partial =\> ‘add\_with\_ajax’

Jason

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 8, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/ror-e-commerce-ajax-wont-fire/78610/4 "2006-12-08T17:07:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 10, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/ror-e-commerce-ajax-wont-fire/78610/5 "2006-12-10T21:23:56Z")

</div>

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.

> **[jlaine.net](https://jlaine.net/)**
>
> Your freedom to carry a gun messes with my freedom to walk around without fear. Your freedom to “stand your ground” with force is at odds with my …

> **[Home](https://dotherightthing.com/)**
>
> do the right thing

[http://www.railsecommerce.com](http://www.railsecommerce.com)  
[http://odesign.fi](http://odesign.fi)

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 10, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/ror-e-commerce-ajax-wont-fire/78610/6 "2006-12-10T21:25:57Z")

</div>

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](http://www.railsecommerce.com)

Thanks,  
//jarkko

–  
Jarkko L.

> **[jlaine.net](https://jlaine.net/)**
>
> Your freedom to carry a gun messes with my freedom to walk around without fear. Your freedom to “stand your ground” with force is at odds with my …

> **[Home](https://dotherightthing.com/)**
>
> do the right thing

[http://www.railsecommerce.com](http://www.railsecommerce.com)  
[http://odesign.fi](http://odesign.fi)
