Topic: [Solved]Railscast 75, Observe_field and Shopping Cart

Hi all,

I have the shopping cart page where a user can add items dynamically through ajax as explained in Railscast episode 75. I am using observe_field to observe the 'quantity' and 'cost' fields to update the total field for each item.
This does not work however, for records that are added through the Ajax. How can i name the fields and observe them as i have explained? is observing the best way to go?
Really appreciate any help.

Last edited by yourstruly_vinay (2008-12-04 01:42:58)

Re: [Solved]Railscast 75, Observe_field and Shopping Cart

Anyone? Im sure its a common problem. I just dont know how to verbalise this better.
For each record, there is a cost field, a quantity field, and a total field. "Cost" and "quantity" are observed and the corresponding "Total" field is updated via an Ajax post to a method called "Calculate" which multiplies the two and updates a div called "Total"(I also need to extract the value of total from this div and assign it to the Total attribute somehow). I show one record in the form when the form is loaded. For this record, this method works. But when i dynamically add more records, their corresponding "Total" divs do not get updated. How can I achieve this? I hope i have explained the problem better and hoping to get some answers.

Re: [Solved]Railscast 75, Observe_field and Shopping Cart

I'm not familiar with dynamically adding more records, but maybe something like this?
<% for item in @items %>
  <div id="cart_for_<%=item%>">
      Cost<%= text_field_tag :quantity %>
      Quantity<%= text_field_tag :cost %>
  <%= observe_field :quantity, :url => { :action => :calculate}, :frequency => 10, :update => "total_for_<%=item%>", :with => :quantity %>
  <%= observe_field :cost, :url => { :action => :calculate}, :frequency => 10, :update => "total_for_<%=item%>", :with => :cost %>

  <div id="total_for_<%=item%>">
    Total<%= text_field_tag :total %>
<% end %>

Re: [Solved]Railscast 75, Observe_field and Shopping Cart

Thanks for your reply Terrance! smile Yeah thats the right way to start. marsvin helped me with a similar problem statement in this thread, if you're interested.