# Help with arrays in forms(getting value from http request pa

**URL:** <https://www.ruby-forum.com/t/help-with-arrays-in-forms-getting-value-from-http-request-pa/90490>\
**Category:** Rails\
**Created:** [March 29, 2007, 6:56am UTC](https://www.ruby-forum.com/t/help-with-arrays-in-forms-getting-value-from-http-request-pa/90490 "2007-03-29T06:56:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elemental](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@elemental](https://www.ruby-forum.com/u/elemental)\
**Post date:** [March 29, 2007, 6:56am UTC](https://www.ruby-forum.com/t/help-with-arrays-in-forms-getting-value-from-http-request-pa/90490/1 "2007-03-29T06:56:16Z")

</div>

Hi all,  
I’m having some trouble getting form data from params, and I could  
use some help. A contact can have an unlimited number of phone  
numbers, so I want the ability to add more phone text boxes (via AJAX)  
as the user clicks add. This works fine, and I get the following  
output by using the “\<%= text\_field “phone” , “number”, “index” =\> n  
%\>” value in my rhtml. This generates the following html.

Number: business fax home mobile Number: business fax home mobile

How do I get the params from “phone” into an array of Phone Objects?  
I’ve tried Array.new(params[:phone]), but this doesn’t work. I’m new  
to ROR so any help would be greatly appreciated.

Thanks,  
Todd

---

<div class="post-metadata">

**Author:** ![elemental](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@elemental](https://www.ruby-forum.com/u/elemental)\
**Post date:** [March 29, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/help-with-arrays-in-forms-getting-value-from-http-request-pa/90490/2 "2007-03-29T14:52:06Z")

</div>

Hi Todd,

Todd N. wrote:

> A contact can have an unlimited number of phone  
> numbers, so I want the ability to add more phone  
> text boxes (via AJAX) as the user clicks add. This  
> works fine, and I get the following output by using  
> the “\<%= text\_field “phone” , “number”, “index” =\> n  
> %\>” value in my rhtml.  
> …  
> How do I get the params from “phone” into an array  
> of Phone Objects? I’ve tried Array.new(params[:phone]),  
> but this doesn’t work.

You might want to check out a thread from yesterday titled “Multiple  
items  
in a single form - try to get radio button selected”. You can use the  
same  
pattern.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![elemental](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@elemental](https://www.ruby-forum.com/u/elemental)\
**Post date:** [March 29, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/help-with-arrays-in-forms-getting-value-from-http-request-pa/90490/3 "2007-03-29T16:32:10Z")

</div>

Thanks for pointing me in the right direction. I’ve been able to  
uniquely name my fields as the thread suggests (see the html from the  
original post). They all have the form “phone[][fieldname]”.  
I’m not sure what the syntax would be to get them out of the https  
params request. Would it simply be

params[":phone[0]"]

to get the first index of the phone input fields?

Thanks,  
Todd

---

<div class="post-metadata">

**Author:** ![elemental](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@elemental](https://www.ruby-forum.com/u/elemental)\
**Post date:** [March 29, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/help-with-arrays-in-forms-getting-value-from-http-request-pa/90490/4 "2007-03-29T16:54:14Z")

</div>

Hi Todd,

Todd N. wrote:

> Thanks for pointing me in the right direction. I’ve been able to  
> uniquely name my fields as the thread suggests (see the html from the  
> original post). They all have the form “phone[][fieldname]”.  
> I’m not sure what the syntax would be to get them out of the https  
> params request. Would it simply be
> 
> params[":phone[0]"]

I’d recommend moving to textfield\_tag and, as Scott did, append the  
index or  
whatever to the name of the param. Your current approach binds your  
text\_field to the model which represents a single row in your table.  
That  
implies that you’ve got multiple phone fields in the record which, given  
that you said ‘unlimited number of phone numbers’, doesn’t sound like  
what  
you want. I’m assuming you’ve got a separate table for your phone  
numbers.  
Yes? If so, then in your Ajax call to add a new field, keep track of  
how  
many you’ve added (possibly in a session variable), then when the form  
is  
submitted, you can iterate through the phone params with something like  
params[:phone#{index}] and put them in the array you’re looking to  
create,  
store them in separate rows, or whatever it is you’re trying to do.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![elemental](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@elemental](https://www.ruby-forum.com/u/elemental)\
**Post date:** [March 29, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/help-with-arrays-in-forms-getting-value-from-http-request-pa/90490/5 "2007-03-29T17:45:25Z")

</div>

Gotcha, that makes a lot more sense now. Thanks for the explanation,  
I’ll try it when I get home tonight and we’ll see if it works!
