Multiple Form Fields with Same Model?

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

Any help would be greatly appreciated.
Thanks.
__
Donnie

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

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?

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).

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

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