Topic: Form submission with multiple records

I'm trying to create a utility for importing an address book into my rails app.  Using FasterCSV, I want to pull rows off an uploaded file and show the user a page that lists each contact and their details.  Here, the user should interact with the page to select which contacts to save to the database.  Then, submission of the form processes the selections, saving the appropriate contacts to the database.

My trouble lies in properly constructing the form and its subsequent processing.  How do I appropriately name and id form elements so that multiple contacts can be passed to the app, and each contact can be saved to the db?

Here's my strategy:

In the view:

<% filename = params[:userFile]["file"].path %> #pass the uploaded csv

<% i = 0 %>

<% form_for(:contact, :url => {:action => 'save'}) do |form| %>
  <% FasterCSV.foreach(filename) do |row| %>
 
  # here, use row|index| to refer to contact name and multiple email addresses
  <%= check_box_tag 'contact', i, checked=false,
    {:name => "contact[name][]", :id => "contact_name_#{i}", :value => row[1] }  %>

  <% i = i + 1 %>
  <% end %>
  <%= submit_tag("Save contacts") %>
 
<% end %>


So, I'm trying to pass contacts to the controller.  Each contact has a name and multiple email addresses.

The controller looks like this:

for item in @params[:contact][:name] do
  @contact = Contact.new(:name => item)
  @contact.save
end

As the code stands right now, I can process multiple names and store them in the database. What I can't figure out is how to add the email addresses to the form.

Can anyone point me in the right direction?  Thanks!

Re: Form submission with multiple records

Have you seen my tutorial on creating multiple models in one form? I think that's what you need.

Railscasts - Free Ruby on Rails Screencasts

Re: Form submission with multiple records

That's a helpful tutorial, but I'm still a bit confused.  In your work, you create 1 project with multiple tasks.

How would the controller/new and view/new change to handle an unknown number of projects, each with an unknown number of tasks?  These numbers are determined by user interaction with the form.