# Multiple Form Fields with Same Model?

**URL:** <https://www.ruby-forum.com/t/multiple-form-fields-with-same-model/63147>\
**Category:** Rails\
**Created:** [June 12, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/multiple-form-fields-with-same-model/63147 "2006-06-12T19:11:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donnie\_Jones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Donnie\_Jones](https://www.ruby-forum.com/u/Donnie_Jones)\
**Post date:** [June 12, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/multiple-form-fields-with-same-model/63147/1 "2006-06-12T19:11:24Z")

</div>

Hello,

I’m trying to create a form that will allow a user to submit a list of  
phone numbers, but the list is dynamically created. Thus allowing my  
form  
to submit an unknown number of phone numbers.

Here is some of the code I have been trying:

\<%= collection\_select( “phone[]”, “phone\_type\_id”,  
PhoneType.find(:all, :order =\> “position”), :id, :description )  
%\>

```
 ( <%= text_field "phone[]", "area_code", :class => "phones" %> )
 <%= text_field "phone[]", "prefix", :class => "phones" %>
 -
 <%= text_field "phone[]", "suffix", :class => "phones" %>
 ext. <%= text_field "phone[]", "extension", :class => "phones" %>

```

\<%= link\_to\_remote(“add”,  
:update =\> “phones-list”,  
:url =\> { :action =\> “add\_phone” },  
:loading =\> “”,  
:complete =\> “”  
)  
%\>

The link\_to\_remote would use AJAX to insert the fields to add a new  
phone  
number, but the issue is that I don’t know how to incorporate an id or  
count field into the fields so controller can tell the diffrence between  
the fields in the params[]. I tried using the phones[] with ‘[]’ based  
upon the agile book, first edition, pg 356 and the rails docs at  
[http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html](http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html)

Any help would be greatly appreciated.  
Thanks.  
\_\_  
Donnie

---

<div class="post-metadata">

**Author:** ![Donnie\_Jones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Donnie\_Jones](https://www.ruby-forum.com/u/Donnie_Jones)\
**Post date:** [August 22, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/multiple-form-fields-with-same-model/63147/2 "2008-08-22T18:32:12Z")

</div>

Donnie Jones wrote:

> Hello,
> 
> I’m trying to create a form that will allow a user to submit a list of  
> phone numbers, but the list is dynamically created. Thus allowing my  
> form  
> to submit an unknown number of phone numbers.
> 
> Here is some of the code I have been trying:
> 
> \<%= collection\_select( “phone[]”, “phone\_type\_id”,  
> PhoneType.find(:all, :order =\> “position”), :id, :description )  
> %\>
> 
> ```
> ( <%= text_field "phone[]", "area_code", :class => "phones" %> )
> <%= text_field "phone[]", "prefix", :class => "phones" %>
> -
> <%= text_field "phone[]", "suffix", :class => "phones" %>
> ext. <%= text_field "phone[]", "extension", :class => "phones" %>
> 
> ```
> 
> \<%= link\_to\_remote(“add”,  
> :update =\> “phones-list”,  
> :url =\> { :action =\> “add\_phone” },  
> :loading =\> “”,  
> :complete =\> “”  
> )  
> %\>
> 
> The link\_to\_remote would use AJAX to insert the fields to add a new  
> phone  
> number, but the issue is that I don’t know how to incorporate an id or  
> count field into the fields so controller can tell the diffrence between  
> the fields in the params[]. I tried using the phones[] with ‘[]’ based  
> upon the agile book, first edition, pg 356 and the rails docs at  
> [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html)
> 
> Any help would be greatly appreciated.  
> Thanks.  
> \_\_  
> Donnie

I know this is knid of old but I have the exact same question actually  
(this post never got answered). I actually found a solution but it is  
kind of ugly and was wondering if there existed an  
easier/prettier/cleaner solution to this problem.  
My solution was to use a sessions (cookies) that would store a counter  
and be incremented every time theres a call to the function that adda a  
new line to the form. This would allow for a unique ID for each entry  
An other solution would be the same thing in JavaScript, that is to  
implement a counter and to make my own JavaScript function that will do  
the same as link\_to\_remote but will insert the index from the counter.  
Any ideas?

---

<div class="post-metadata">

**Author:** ![Donnie\_Jones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Donnie\_Jones](https://www.ruby-forum.com/u/Donnie_Jones)\
**Post date:** [August 22, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/multiple-form-fields-with-same-model/63147/3 "2008-08-22T19:17:21Z")

</div>

Shandy N. wrote:

> The only way I could think about how to do something like this would be  
> to have a table relating users to a phone number, that way they could  
> have as many phone numbers as they wanted. Then as they added a number  
> if would get entered into the list, your method would go out and get the  
> numbers (including the one you just added) and then render a partial  
> inside of a div. Not sure if this helps,
> 
> -S

Yes that is correct I already have a table relating in my case recipes  
to ingredients. The problem is I want to save the ingredients (or in the  
case of the example above the phone numbers) only when the whole form is  
submitted, hence the need to index each line of the form. The problem  
here is to find an easy way to index each ilne of the form. (I hope I’m  
being clear, I’m not english).

---

<div class="post-metadata">

**Author:** ![Donnie\_Jones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Donnie\_Jones](https://www.ruby-forum.com/u/Donnie_Jones)\
**Post date:** [August 22, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/multiple-form-fields-with-same-model/63147/4 "2008-08-22T19:09:08Z")

</div>

Catherine … wrote:

> Donnie Jones wrote:
> 
> > Hello,
> > 
> > I’m trying to create a form that will allow a user to submit a list of  
> > phone numbers, but the list is dynamically created. Thus allowing my  
> > form  
> > to submit an unknown number of phone numbers.
> > 
> > Here is some of the code I have been trying:
> > 
> > \<%= collection\_select( “phone[]”, “phone\_type\_id”,  
> > PhoneType.find(:all, :order =\> “position”), :id, :description )  
> > %\>
> > 
> > ```
> > ( <%= text_field "phone[]", "area_code", :class => "phones" %> )
> > <%= text_field "phone[]", "prefix", :class => "phones" %>
> > -
> > <%= text_field "phone[]", "suffix", :class => "phones" %>
> > ext. <%= text_field "phone[]", "extension", :class => "phones" %>
> > 
> > ```
> > 
> > \<%= link\_to\_remote(“add”,  
> > :update =\> “phones-list”,  
> > :url =\> { :action =\> “add\_phone” },  
> > :loading =\> “”,  
> > :complete =\> “”  
> > )  
> > %\>
> > 
> > The link\_to\_remote would use AJAX to insert the fields to add a new  
> > phone  
> > number, but the issue is that I don’t know how to incorporate an id or  
> > count field into the fields so controller can tell the diffrence between  
> > the fields in the params[]. I tried using the phones[] with ‘[]’ based  
> > upon the agile book, first edition, pg 356 and the rails docs at  
> > [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html)
> > 
> > Any help would be greatly appreciated.  
> > Thanks.  
> > \_\_  
> > Donnie

The only way I could think about how to do something like this would be  
to have a table relating users to a phone number, that way they could  
have as many phone numbers as they wanted. Then as they added a number  
if would get entered into the list, your method would go out and get the  
numbers (including the one you just added) and then render a partial  
inside of a div. Not sure if this helps,

-S
