Pulling my hair out: complex collection_select

I've been trying to extend Ryan Bates' great recipe from Advanced Rails Recipes called "Handle Multiple Models In One Form". I got everything in my "base" functionality to work in about 30 minutes and I've spent the last 4 days trying to figure out how to use collection_select in the forms - very frustrating!

The basic idea of his recipe is here:

I'm building a simple address book so I have models Person and EmailAddress where a person has_many email_addresses. My edit form, like Ryan's, has the ability to add new fields with page.insert_html and to remove existing entries. All's fine up to this point.

What I can't do is create a drop-down menu with a simple list of options to tag the email. Here's what I think should work in my email partial (called from <%= render :partial => 'person/email', :collection => @person.email_addresses %>) :

<% new_or_existing = email.new_record? ? 'new' : 'existing' %> <% prefix = "person[#{new_or_existing}_email_attributes]"%> <% fields_for prefix, email do |email_form| %>    <%= email_form.text_field :email %>&nbsp;    <%= email_form.collection_select(:person, "email_attributes", EmailAddress::VALID_EMAIL_TAGS, "to_s", "to_s") %> [...snip...] <% end %>

The error I get is "undefined method `merge' for "to_s":String"

If I replace the collection_select call with this: <select name="test"> <%= options_from_collection_for_select(EmailAddress::VALID_EMAIL_TAGS,       "to_s",       "to_s",       email.tag) %> </select>

I get as many email partials rendered as there are items in the collection (and the dynamic insert_html works too). The problem is that all the select input fields are named "test" and I don't get them passed in my params hash as part of the EmailAddress record. This breaks the mass assignment possibilities and ruins so much of the benefit of Ryan's recipe.

The text field works great and I get one of these two kinds of params: person[existing_email_attributes][4][tag] person[new_email_attributes][tag]

Any ideas?

Justin Rowe wrote:

I've been trying to extend Ryan Bates' great recipe from Advanced Rails Recipes called "Handle Multiple Models In One Form". I got everything in my "base" functionality to work in about 30 minutes and I've spent the last 4 days trying to figure out how to use collection_select in the forms - very frustrating!

5 minute rule. I post and then I get it working. Basically I gave up on getting a helper function to work for the field name and just created it myself and used the options_from_collection_for_select instead:

<select name="person[<%= new_or_existing %>_email_attributes][<%= email.id %>][tag]">    <%= options_from_collection_for_select(EmailAddress::VALID_EMAIL_TAGS,       "to_s",       "to_s",       email.tag) %> </select>

Oy.