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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Oct 24, 12:31 pm, Vinay <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The cost you should get when you get to the controller... Just send the id of the product and find the cost when you get to the controller... About the quantity, not sure about the observer but I just added a remote_form that has the quantity property: <% remote_form_for :quantity, :url => { :controller => "carts", :action => :add, :id => @product.id }, :complete => visual_effect(:highlight, ''floating_cart'') do |f| -%> <%= f.select :quantity, (1..5) %> <%= image_submit_tag("../images/add_to_cart.png", :class => "submit_tag") %> <% end %> This is for the cart model... adding a product to the cart... item.update_attributes({ :amount => item.amount + quantity, :price => product.price, :weight => product.weight}) cart_items << item --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Freddy! Although what I need help is with a different issue. Let me try to explain better. The quantity and cost fields are not the problem. The "Total" field is. This field is to get updated with cost*qty, as and when the user changes the qty. This is working successfully for me when i have a record of just one product in the cart. But when i add more products through an "Add product" ajax link, their respective "Total" fields dont work. I understand that i have to index every product i add to the cart in order for each of them to have an identity. But according to the Railscast 75, the accepted parameters cannot be processed in a setter method if the index is included in the array of attributes. Hope that makes it a bit clearer. Help! On Oct 26, 3:38 am, Freddy Andersen <fre...-RCI/mp9mI1I6GGFevw1D/A@public.gmane.org> wrote:> The cost you should get when you get to the controller... Just send > the id of the product and find the cost when you get to the > controller... > > About the quantity, not sure about the observer but I just added a > remote_form that has the quantity property: > > <% remote_form_for :quantity, :url => { :controller => > "carts", :action => :add, :id => @product.id }, > :complete => visual_effect(:highlight, ''floating_cart'') do |f| -%> > <%= f.select :quantity, (1..5) %> > <%= image_submit_tag("../images/add_to_cart.png", :class => > "submit_tag") %> > <% end %> > > This is for the cart model... adding a product to the cart... > item.update_attributes({ :amount => item.amount + quantity, > :price => product.price, > :weight => product.weight}) > cart_items << item--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
has noone got a pointer? I need a fair idea of how to go about this. Any help at all will be useful. Thanks! On Oct 28, 10:26 am, Vinay <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Freddy! > Although what I need help is with a different issue. Let me try to > explain better. > The quantity and cost fields are not the problem. The "Total" field > is. This field is to get updated with cost*qty, as and when the user > changes the qty. This is working successfully for me when i have a > record of just one product in the cart. But when i add more products > through an "Add product" ajax link, their respective "Total" fields > dont work. > I understand that i have to index every product i add to the cart in > order for each of them to have an identity. But according to theRailscast75, the accepted parameters cannot be processed in a setter > method if the index is included in the array of attributes. > Hope that makes it a bit clearer. Help! > > On Oct 26, 3:38 am, Freddy Andersen <fre...-RCI/mp9mI1I6GGFevw1D/A@public.gmane.org> wrote: > > > The cost you should get when you get to the controller... Just send > > the id of the product and find the cost when you get to the > > controller... > > > About the quantity, not sure about the observer but I just added a > > remote_form that has the quantity property: > > > <% remote_form_for :quantity, :url => { :controller => > > "carts", :action => :add, :id => @product.id }, > > :complete => visual_effect(:highlight, ''floating_cart'') do |f| -%> > > <%= f.select :quantity, (1..5) %> > > <%= image_submit_tag("../images/add_to_cart.png", :class => > > "submit_tag") %> > > <% end %> > > > This is for the cart model... adding a product to the cart... > > item.update_attributes({ :amount => item.amount + quantity, > > :price => product.price, > > :weight => product.weight}) > > cart_items << item--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---