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:
http://railscasts.com/episodes/75
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 %>
<%= 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?
--
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
-~----------~----~----~----~------~----~------~--~---