Creating an array of params

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

try something like this:

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

Steve

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.

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

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