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

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.

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

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

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.

On Jul 3, 10:12 am, Jean N. [email protected]
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

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

On 3 Èec 2007, 05:01, Jean N. [email protected]

Jeff C. wrote:

On Jul 3, 10:12 am, Jean N. remove[email protected]
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

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. :wink:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs