# Associating an array to multiple text fields in a form

**URL:** <https://www.ruby-forum.com/t/associating-an-array-to-multiple-text-fields-in-a-form/108373>\
**Category:** Ruby\
**Created:** [July 2, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/associating-an-array-to-multiple-text-fields-in-a-form/108373 "2007-07-02T21:37:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 2, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/associating-an-array-to-multiple-text-fields-in-a-form/108373/1 "2007-07-02T21:37:05Z")

</div>

I have a model that has the following classes: ShoppingList, Item and  
Product.

– _Product_ is just a list of product names: Apple, Orange, Banana,  
Pear,  
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  
_Product_.

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| %\>

\<%= text\_field("shopping\_list.items[" + item.id.to\_s + "].quantity", item.quantity, :size =\> 3) %\> \<%= (Product.find item.product\_name\_id).name%\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 4, 2007, 5:39am UTC](https://www.ruby-forum.com/t/associating-an-array-to-multiple-text-fields-in-a-form/108373/2 "2007-07-04T05:39:08Z")

</div>

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.

Thanks.
