matthew newhook
2005-Oct-29 02:08 UTC
JavaScriptHelper::update_element_function & multiple AJAX updates?
Hi, I''ve been developing (gasp) a shopping cart with AJAX. My problem is that I want to add a new item, and update the total at the same time which means that AJAX call needs to update multiple items. The JavaScriptHelper::update_element_function seems to be the right thing to use, but the example in the doc is less than obvious. The cart itself is held in a side-bar. Currently my code updates the content of the cart itself, but not the total -- which is the problem I want to solve :) The form to add a new item to the cart is: <%= form_remote_tag :url => { :action => ''add_to_cart'', :id => order.product}, :update => "cart", :position => :bottom %> Unit Price <%= order.product.price %> <p/> <label for="cartitem_quantity">Quantity </label> <%= select(:cartitem, :quantity, %w{ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 }) %> <p/> <label for="cartitem_instructions">Instructions</label><br/> <%= text_area :cartitem, :instructions, :rows => 5 %> <p/> <%= hidden_field :cartitem, :id, :value => order.product.id<http://order.product.id>%> <%= submit_tag ''add item to order'' %> <%= end_form_tag %> The shopping cart view looks like: <h2>Your Order</h2> <table cellpadding=1 cellspacing=0 id="cart"> <% if @cart.items.length == 0 %> <h2>Cart is empty</h2> <% else %> <tr class="carttitle"> <td>Qty</td> <td>Item</td> <td>Total</td> </tr> <%= render(:partial => ''cartitem'', :collection => @cart.items) %> </table> <% end %> <table cellpadding=1 cellspacing=0> <tr> <td colspan="2" align="left"><strong>Total:</strong></td> <td id="totalcell"><%= @cart.total_price %></td> </tr> </table> And the current code to add a new item to the cart is: def add_to_cart @cartitem = CartItem.new(params[:cartitem]) if @cartitem.validate @cart.add(@cartitem) render(:partial => "cartitem", :object => @cartitem, :layout => false) else redirect_to :action => ''order'', :id => params[:id] end end Any ideas? hints? examples? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
matthew newhook
2005-Nov-01 00:44 UTC
Re: JavaScriptHelper::update_element_function & multiple AJAX updates?
Anyone out there know how to do this? M On 10/29/05, matthew newhook <matthew.newhook-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > I''ve been developing (gasp) a shopping cart with AJAX. My problem is that > I want to add a new item, and update the total at the same time which means > that AJAX call needs to update multiple items. The > JavaScriptHelper::update_element_function seems to be the right thing to > use, but the example in the doc is less than obvious. > > The cart itself is held in a side-bar. Currently my code updates the > content of the cart itself, but not the total -- which is the problem I want > to solve :) > > The form to add a new item to the cart is: > > <%= form_remote_tag :url => { :action => ''add_to_cart'', :id => > order.product }, > :update => "cart", :position => :bottom %> > > Unit Price <%= order.product.price %> > <p/> > <label for="cartitem_quantity">Quantity </label> > <%= select(:cartitem, :quantity, %w{ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 > 16 17 18 19 20 }) %> > <p/> > <label for="cartitem_instructions">Instructions</label><br/> > <%= text_area :cartitem, :instructions, :rows => 5 %> > <p/> > <%= hidden_field :cartitem, :id, :value => order.product.id<http://order.product.id>%> > <%= submit_tag ''add item to order'' %> > <%= end_form_tag %> > > The shopping cart view looks like: > > <h2>Your Order</h2> > <table cellpadding=1 cellspacing=0 id="cart"> > <% if @cart.items.length == 0 %> > <h2>Cart is empty</h2> > <% else %> > <tr class="carttitle"> > <td>Qty</td> > <td>Item</td> > <td>Total</td> > </tr> > <%= render(:partial => ''cartitem'', :collection => @cart.items) %> > </table> > <% end %> > <table cellpadding=1 cellspacing=0> > <tr> > <td colspan="2" align="left"><strong>Total:</strong></td> > <td id="totalcell"><%= @cart.total_price %></td> > </tr> > </table> > > And the current code to add a new item to the cart is: > > def add_to_cart > @cartitem = CartItem.new(params[:cartitem]) > if @cartitem.validate > @cart.add(@cartitem) > render(:partial => "cartitem", :object => @cartitem, :layout => false) > else > redirect_to :action => ''order'', :id => params[:id] > end > end > > Any ideas? hints? examples? >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails