Q on AJAX with respect to DRY

I have a shopping cart function, and I’d like to set it up to update the
cart with AJAX. My AJAX call will return a view that displays the cart
items.

My question is with regards to keeping things DRY. When the user goes to
“view cart”, that page needs to display the cart, then the cart display
needs to update with AJAX calls when items are added from that page. It
would seem, at first glance, that I’ll have to put the HTML code to
display the cart in two places: in the “view cart” view and in the
“update cart” AJAX view.

My thought is to put the cart display into a partial, and render the
partial from both places. Will that work in the view that the AJAX call
returns?

I ask because I initially had some trouble with a simple dynamic refresh
using AJAX. With “render :layout => false” in the controller, the
instance variables do not appear to get passed from controller to view.
For instance, I tried this:

controller:
def get_live_price
@price = Frame.find(:first, :conditions => [“blah = ?”,
params[:frame]]).price
render :layout => false
end

view:
<%= @price %>

…and it didn’t work - I got errors about the instance variable not
existing.

eventually I ended up with the following, which works:

controller:
def get_live_price
render :layout => false
end

<%= number_to_currency(Frame.find(:first, :conditions => [“blah”,
params[:frame]]).price) %>

But this feels wrong to me, with the find logic in the view like that.

I sincerely appreciate any assistance or guidance.

My question is with regards to keeping things DRY. When the user goes to
“view cart”, that page needs to display the cart, then the cart display
needs to update with AJAX calls when items are added from that page. It
would seem, at first glance, that I’ll have to put the HTML code to
display the cart in two places: in the “view cart” view and in the
“update cart” AJAX view.

That’s what partials are for. Bulid a _cart.rhtml, which contains code
like:

<% for item in cart.items %>
<%= item.name %>
<% end %>

and include it in view.rhtml:

<%= render :partial=>‘cart’, :locals=>{ :cart=>@cart } %>

in your update method in the controller:

def update
@cart = <retrieve cart from sesion, or db, or wherever it lives>
… update the cart …
render :partial=>‘cart’, :locals=>{ :cart=>@cart }
end

I’d also suggest getting a copy of the Agile Web D. with
Rails book - that has code for an Ajax’d shopping cart and is
generally a very good book.

Cheers,
Max

Thanks Max. I have AWDWR, PDF, but the AJAX stuff must’ve been added in
latest version cause I had not seen it before in the “build an
application section”.

I appreciate the help.
c.

Max M. wrote:

My question is with regards to keeping things DRY. When the user goes to
“view cart”, that page needs to display the cart, then the cart display
needs to update with AJAX calls when items are added from that page. It
would seem, at first glance, that I’ll have to put the HTML code to
display the cart in two places: in the “view cart” view and in the
“update cart” AJAX view.

That’s what partials are for. Bulid a _cart.rhtml, which contains code
like:

<% for item in cart.items %>
<%= item.name %>
<% end %>

and include it in view.rhtml:

<%= render :partial=>‘cart’, :locals=>{ :cart=>@cart } %>

in your update method in the controller:

def update
@cart = <retrieve cart from sesion, or db, or wherever it lives>
… update the cart …
render :partial=>‘cart’, :locals=>{ :cart=>@cart }
end

I’d also suggest getting a copy of the Agile Web D. with
Rails book - that has code for an Ajax’d shopping cart and is
generally a very good book.

Cheers,
Max