Forum: Ruby associating an array to multiple text fields in a form

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
B199c6556fec872d272086be15c5e4ab?d=identicon&s=25 Robert La ferla (rlaferla)
on 2007-07-02 21:37
(Received via mailing list)
I have a model that has the following classes: ShoppingList, Item and

-- *Product* is just a list of product names:  Apple, Orange, Banana,
Grape.  The table has two columns: ID and NAME.

-- A *ShoppingList* has a name (string) and many *Item* objects.

-- Each *Item* has a *quantity* (integer) and to-one relationship to a

My ShoppingList's edit view has a form that contains a textfield for the
name of
a shopping list and a table of items and their quantities:

Name:  MyShoppingList

Qty.  Item
3      Apples
0      Oranges
1      Pear

The model object for this view is, of course, a ShoppingList.

I am trying to bind each text field to the quantity attribute of each
item in the shopping list.  I tried something like this but it doesn't
work because Rails wants an instance variable to bind to.  How do I
properly bind my array?

<% @shopping_list.items.each do |item| %>
<td><%= text_field("shopping_list.items[" + + "].quantity",
item.quantity, :size => 3) %></td><td><%= (Product.find
<% end %>
B199c6556fec872d272086be15c5e4ab?d=identicon&s=25 Robert La ferla (rlaferla)
on 2007-07-04 05:39
(Received via mailing list)
I meant to post this to the Rails mailing list not this one.  Sorry.
Please don't respond to the list but rather to me personally.

This topic is locked and can not be replied to.