1

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?

Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
DNB5brims
  • 29,344
  • 50
  • 131
  • 195

2 Answers2

1

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

Community
  • 1
  • 1
Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
1

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
  • 106
  • 1
  • 4
  • It seems great, but I only want one "Make Order" submit button in my form, how can I do that? – DNB5brims Dec 27 '09 at 14:11
  • 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 Dec 27 '09 at 14:26
  • it's a question of style, but personally i would enumerate @products using the each enumerator method. – Steve Graham Dec 27 '09 at 22:44