views:

41

answers:

2

I use this to loop the products...

<% form_for :product, @products, :url => { :action => "add_to_cart" } do |f| %>
<%= product.title %>
<%= product.price %>
<%= submit_tag 'Make Order' %>
<% end %>

In my DB, I have

product: 
title:abc price:874
title:cde price:98
title:efg price:18

but I can only get the efg 18.0 in my result, I miss other records on my result, any ideas on that?

+1  A: 

form_for creates a form for a single object. If you want to create a form for multiple objects, I suggest you take a look at this question: Multiple objects in a Rails form

Can Berk Güder
+1  A: 

I suppose that you need an extra form for each product, (based on the add_to_cart action).
form_for helper generates a form for one object, so you will need to iterate through your objects and create a form for each one.

Something like that is probably what you need:

<% for product in @products %>
  <% form_for product, :url => { :action => "add_to_cart" } do |f| %>
    <%= product.title %>
    <%= product.price %>
    <%= f.submit 'Make Order' %>
  <% end %>
<% end %>
apod
It seems great, but I only want one "Make Order" submit button in my form, how can I do that?
Ted Wong
How do you select the products to be added on your cart ? If you are adding these products dynamically into the form from ajax, then you should follow Can Berk Güder advice.
apod
@Ted: see the code sample in the question from my answer.
Can Berk Güder
it's a question of style, but personally i would enumerate @products using the each enumerator method.
Steve Graham