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.
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.
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.
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
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.