# Creating an array of params

**URL:** <https://www.ruby-forum.com/t/creating-an-array-of-params/62299>\
**Category:** Rails\
**Created:** [June 2, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-params/62299 "2006-06-02T13:51:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 2, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-params/62299/1 "2006-06-02T13:51:39Z")

</div>

I’ve got a bunch of text fields that I’d like to access as an array.  
The code looks like this:

\<%= text\_field\_tag “people[0][entry]” %\>  
\<%= text\_field\_tag “people[1][entry]” %\>  
\<%= text\_field\_tag “people[2][entry]” %\>

So when I submit the form, I’d like to be able to use it like  
params[:people].each {|p| p[:entry]}

However when I submit it, it can’t build the params list - blows up with  
undefined method `update’ for “2”:String

Any ideas on how to do this?

Thanks,  
Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 2, 2006, 2:01pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-params/62299/2 "2006-06-02T14:01:00Z")

</div>

try something like this:

params[:people].each {|index, data| data[:entry]}

Steve

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [June 2, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-params/62299/3 "2006-06-02T14:16:33Z")

</div>

Pat M. wrote:

> However when I submit it, it can’t build the params list - blows up with  
> undefined method `update’ for “2”:String

The way you’ve done it, params[:people] will be a hash with  
integer keys. So you need to code:

```
params[:people].each_value {|p| p[:entry]}

```

If you want an array you instead need to code:

\<%= text\_field\_tag “people[entry][]” %\>  
\<%= text\_field\_tag “people[entry][]” %\>  
\<%= text\_field\_tag “people[entry][]” %\>

params[:people][:entry].each {|p| p}

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 15, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-params/62299/4 "2007-08-15T18:01:30Z")

</div>

yaman wrote:

> but how can i get access to the value I gave to people? like 0, 1 or 2  
> I put there ID’s from the DB so it is not always 0 to …  
> and in my controller I’ll need access to them. Any suggestions?
> 
> Thanks in advance

Here’s how I’ve done a similar thing. I’m using a partial in the form  
(both edit and create)to dynamically change the number of  
alternate\_flows (same as your people). The partial is rendered multiple  
times with varying “index” values to distinguish the params:

 \<% fields\_for "alternate\_flows[#{index}]", flow do |field| %\> 

Alternate Flow

\<%= field.text\_area :steps -%\>

 \<% end %\>

The params that are fed to my create/update actions in the controller  
include:

#{

# “alternate\_flows”=\>

# {

# “0”=\>{“steps”=\>“foo”},

# “1”=\>{“steps”=\>“bar”},

# “2”=\>{“steps”=\>“baz”}

# },

And you can access both the index and the values (“steps” in my case) as  
follows:

```
    params[:alternate_flows].each do |index, alt_flow|
      flash[index] = "index: " + index +" steps: " + 

```

alt\_flow[:steps]  
end

The “each” iterator provides a handle on both the index (0,1,2) and the  
hash associated with that index. In my example, there is only one value  
“steps”, but there could be any number.

Hope that helps.

Scott

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 1, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/creating-an-array-of-params/62299/5 "2007-04-01T15:17:57Z")

</div>

Pat M. wrote:

> I’ve got a bunch of text fields that I’d like to access as an array.  
> The code looks like this:
> 
> \<%= text\_field\_tag “people[0][entry]” %\>  
> \<%= text\_field\_tag “people[1][entry]” %\>  
> \<%= text\_field\_tag “people[2][entry]” %\>
> 
> So when I submit the form, I’d like to be able to use it like  
> params[:people].each {|p| p[:entry]}
> 
> However when I submit it, it can’t build the params list - blows up with  
> undefined method `update’ for “2”:String
> 
> Any ideas on how to do this?
> 
> Thanks,  
> Pat

I got that I can access the params with something like:

params[:people].each\_value |p|  
print p[:entry]  
end

but how can i get access to the value I gave to people? like 0, 1 or 2  
I put there ID’s from the DB so it is not always 0 to …  
and in my controller I’ll need access to them. Any suggestions?

Thanks in advance
