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.