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 Maddox 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Pat Maddox 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":StringThe 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.
Pat Maddox 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, > PatI 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 advanceHere''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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---