# Rails, partials and AJAX.. what am I missing?

**URL:** <https://www.ruby-forum.com/t/rails-partials-and-ajax-what-am-i-missing/108399>\
**Category:** Rails\
**Created:** [July 3, 2007, 5:01am UTC](https://www.ruby-forum.com/t/rails-partials-and-ajax-what-am-i-missing/108399 "2007-07-03T05:01:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 3, 2007, 5:01am UTC](https://www.ruby-forum.com/t/rails-partials-and-ajax-what-am-i-missing/108399/1 "2007-07-03T05:01:07Z")

</div>

Hi

I’ve had this problem before, but the previous solution isn’t working so  
I believe I’m just missing something in the ‘chain’ mentioned in the  
subject.

Nutshell summary: I’m removing items from a cart and I have an rjs  
defined to ‘shake’ my cart when something is removed. The shake happens  
but the refresh of the cart doesn’t. If I manually hit refresh in my  
browser the page is redrawn and the totals in the cart are correct.

Some ‘snipped’ code from my flow.

The index.page (button invoking the AJAX call)

\<% form\_remote\_tag :url =\> { :action =\> :remove\_from\_cart, :id =\>  
product } do %\>  
\<%= submit\_tag “Remove from Cart” %\>  
\<% end %\>

Controller called…

def remove\_from\_cart  
begin  
product = Product.find( params[:id])  
rescue ActiveRecord::RecordNotFound  
# stuff here…  
else  
@cart = find\_cart  
@current\_item = @cart.remove\_product(product)  
end  
# I have tried with this removed (letting the remove\_from\_cart.rjs  
be called automatically)  
redirect\_to\_index  
end

Model code…

def remove\_product( product )  
current\_item = @items.find { |item| item.product == product }  
if current\_item  
current\_item.decrease\_quantity  
@items.delete(current\_item) if current\_item.quantity \<= 0  
current\_item  
else  
product  
end  
end

Lastly the remove\_from\_cart.rjs

page[:cart].visual\_effect :shake

As mentioned, I just am not grasping when the actual CART is being  
refreshed, or what FIRE the refresh.

Thanks.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 3, 2007, 9:13am UTC](https://www.ruby-forum.com/t/rails-partials-and-ajax-what-am-i-missing/108399/2 "2007-07-03T09:13:02Z")

</div>

> Lastly the remove\_from\_cart.rjs
> 
> page[:cart].visual\_effect :shake
> 
> As mentioned, I just am not grasping when the actual CART is being  
> refreshed, or what FIRE the refresh.
> 
> Thanks.

you need to update the html itself of the cart. the updating of the cart  
takes place perfectly, but you need to transfer the new details back.  
(via rjs/AJAX of course):

page[:cart].visual\_effect :shake

# HERE you need to add the new details of the cart

# ((should replace the more updated cart details:))

page.replace\_html ‘cart’, :partial =\> ‘cart’

# or whatever you call the partial that renders the cart details

hth,

shai

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 3, 2007, 10:04am UTC](https://www.ruby-forum.com/t/rails-partials-and-ajax-what-am-i-missing/108399/3 "2007-07-03T10:04:32Z")

</div>

> Model code…
> 
> def remove\_product( product )  
> current\_item = @items.find { |item| item.product == product }  
> if current\_item  
> current\_item.decrease\_quantity  
> @items.delete(current\_item) if current\_item.quantity \<= 0  
> current\_item  
> else  
> product  
> end  
> end

The answer of Shai its ok.  
And I have to add that if you are storing model objects directly in the  
session you can have troubles… better store only the ids. Because if  
you have made changes in the model some day and deployed it to  
production…, all the model objects in the sessions at this time are  
out of date… think about it

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 3, 2007, 5:12pm UTC](https://www.ruby-forum.com/t/rails-partials-and-ajax-what-am-i-missing/108399/4 "2007-07-03T17:12:27Z")

</div>

Moises D. wrote:

> > Model code…
> > 
> > def remove\_product( product )  
> > current\_item = @items.find { |item| item.product == product }  
> > if current\_item  
> > current\_item.decrease\_quantity  
> > @items.delete(current\_item) if current\_item.quantity \<= 0  
> > current\_item  
> > else  
> > product  
> > end  
> > end
> 
> The answer of Shai its ok.  
> And I have to add that if you are storing model objects directly in the  
> session you can have troubles… better store only the ids. Because if  
> you have made changes in the model some day and deployed it to  
> production…, all the model objects in the sessions at this time are  
> out of date… think about it

Does the order in which an AJAX call appears effect when it’s fired (in  
the RJS?).

I understood (maybe wrongfully) that the code in the RJS is a definition  
only, if that’s true when is the call actually fired?

(sort of answering my own question…) it could be a button, link or  
something similar but how would a ‘refresh’ of my cart be fired off?

Thanks.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 3, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/rails-partials-and-ajax-what-am-i-missing/108399/5 "2007-07-03T21:54:22Z")

</div>

On Jul 3, 10:12 am, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I understood (maybe wrongfully) that the code in the RJS is a definition  
> only, if that’s true when is the call actually fired?
> 
> (sort of answering my own question…) it could be a button, link or  
> something similar but how would a ‘refresh’ of my cart be fired off?

It took me a little while to wrap my head around this too, but the RJS  
is translated to Javascript automatically for you, returned to the  
browser, and then executed in the browser - and all this happens  
asynchronously with regards to user actions in the browser.

Now, what makes everything fire off to begin with, is just as you said

- a button, link, or internal timer (like with  
periodically\_call\_remote).

Getting back to your original problem - what happens if you comment  
out the shake effect, and only do the div replacement with your  
partial. Does the cart update correctly?

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [May 25, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/rails-partials-and-ajax-what-am-i-missing/108399/6 "2008-05-25T21:55:14Z")

</div>

oh my godness! this helped me from bloody hell… thanks a million

On 3 Èec 2007, 05:01, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 4, 2007, 1:26am UTC](https://www.ruby-forum.com/t/rails-partials-and-ajax-what-am-i-missing/108399/7 "2007-07-04T01:26:07Z")

</div>

Jeff C. wrote:

> On Jul 3, 10:12 am, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I understood (maybe wrongfully) that the code in the RJS is a definition  
> > only, if that’s true when is the call actually fired?
> > 
> > (sort of answering my own question…) it could be a button, link or  
> > something similar but how would a ‘refresh’ of my cart be fired off?
> 
> It took me a little while to wrap my head around this too, but the RJS  
> is translated to Javascript automatically for you, returned to the  
> browser, and then executed in the browser - and all this happens  
> asynchronously with regards to user actions in the browser.
> 
> Now, what makes everything fire off to begin with, is just as you said
> 
> - a button, link, or internal timer (like with  
> periodically\_call\_remote).
> 
> Getting back to your original problem - what happens if you comment  
> out the shake effect, and only do the div replacement with your  
> partial. Does the cart update correctly?
> 
> Jeff  
> [softiesonrails.com](http://softiesonrails.com)

Thanks so much for the ‘under the hood’ details info like this REALLY  
clarifies what happens to me. The problem I had was I actually wasn’t  
refreshing the DIV (Using page.replace\_html) for the cart in the  
remove\_from\_cart.rjs template (doh!).

That’s all fixed, and now I have another thread with more of my issues.  
lol. Namely calling a method\_a that called method\_b and rendering the  
rjs in method\_b NOT method\_a.

I’m not having much luck. 😉

It’s the thread entitles “Method chaining and RJS templates”

All in all, I am LOVING this framework, I find coding fun again after  
about 7 years of Java and it’s various technologies.
