Forum: Ruby on Rails Creating an array of params

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Pat M. (Guest)
on 2006-06-02 15:51
(Received via mailing list)
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
Stephen B. (Guest)
on 2006-06-02 16:01
(Received via mailing list)
try something like this:

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

Steve
Mark Reginald J. (Guest)
on 2006-06-02 16:16
(Received via mailing list)
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 (Guest)
on 2007-04-01 17:17
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
Scott S. (Guest)
on 2007-08-15 20:01
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:

<div id="alt_flow_<%= index %>">
  <% fields_for "alternate_flows[#{index}]", flow do |field| %>
    <p><label for="alternate_flows[<%= index -%>]">Alternate
Flow</label></p>
    <p><%= field.text_area :steps -%></p>
  <% end %>
</div>

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
This topic is locked and can not be replied to.