Todd Nine
2007-Mar-29 04:55 UTC
Help with arrays in forms(getting value from http request params)
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. <tr><td>Number:</td><td> <input id="phone_0_number" name="phone[0] [number]" size="30" type="text" /></td> <td><select id="phone_0_phone_type_id" name="phone[0][phone_type_id]"><option value="3">business</option> <option value="4">fax</option> <option value="2">home</option> <option value="1">mobile</option></select></td></tr> <tr><td>Number:</td><td> <input id="phone_1_number" name="phone[1] [number]" size="30" type="text" /></td> <td><select id="phone_1_phone_type_id" name="phone[1][phone_type_id]"><option value="3">business</option> <option value="4">fax</option> <option value="2">home</option> <option value="1">mobile</option></select></td></tr> 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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bill Walton
2007-Mar-29 12:51 UTC
Re: Help with arrays in forms(getting value from http request params)
Hi Todd, Todd Nine 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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Todd Nine
2007-Mar-29 14:30 UTC
Re: Help with arrays in forms(getting value from http request params)
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[<index>][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 On Mar 29, 8:51 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> Hi Todd, > > > > Todd Nine 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--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bill Walton
2007-Mar-29 14:52 UTC
Re: Help with arrays in forms(getting value from http request params)
Hi Todd, Todd Nine 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[<index>][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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Todd Nine
2007-Mar-29 15:43 UTC
Re: Help with arrays in forms(getting value from http request params)
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! On Mar 29, 10:52 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> Hi Todd, > > Todd Nine 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[<index>][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--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---