Pardee, Roy
2008-May-02 20:52 UTC
names of select elements inside a fields_for block not generated as expected
Hey All,
I''m trying to play along w/the ''complex forms 1''
railscast
(http://railscasts.com/episodes/73) and having trouble. The view is
projects/new. I''m trying to add some project_people to the form w/code
like so:
<% form_for(@project) do |f| %>
[project stuff here]
<% for pp in @project.project_person %>
<% fields_for ''project[proj_people_attributes][]'', pp
do |pp_form|
%>
<p>
Person:
<%= collection_select(:pp, :person_id, Person.get_list, :id,
:nom, {:prompt => ''Person?''}) %>
Role:
<%= select(:pp, :role, Person::ROLE_NAMES, {:prompt =>
''Role?''})
%>
</p>
<% end %>
<% end %>
<% end %>
I get the expected series of 3 <select>s, but their names are not
automatically indexed. So I get:
<select id="pp_person_id" name="pp[person_id]">
<select id="pp_role" name="pp[role] ">
Rather than this:
<select id="pp_person_id"
name="project[proj_people_attributes][1][person_id]">
<select id="pp_role"
name="project[proj_people_attributes][1][role] ">
(At least I *think* that''s what they should be.)
Now, you may be saying to yourself "he''s got to call those select
helpers on the pp_form object that the fields_for block yields." If I
do that, I get (e.g.): undefined method `merge'' for :nom:Symbol. So
are
there different select helpers for using inside fields_for?
Many thanks!
-Roy
Roy Pardee
Research Analyst/Programmer
Group Health Center For Health Studies (Cancer Research Network)
(206) 287-2078
Google Talk: rpardee
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Roy Pardee
2008-May-02 21:42 UTC
Re: names of select elements inside a fields_for block not generated as expected
On May 2, 1:52 pm, "Pardee, Roy" <parde...-go57ItdSaco@public.gmane.org> wrote:> Hey All, > > I''m trying to play along w/the ''complex forms 1'' railscast > (http://railscasts.com/episodes/73) and having trouble. The view is > projects/new. I''m trying to add some project_people to the form w/code > like so: > > <% form_for(@project) do |f| %> > > [project stuff here] > > <% for pp in @project.project_person %> > <% fields_for ''project[proj_people_attributes][]'', pp do |pp_form| > %> > <p> > Person: > <%= collection_select(:pp, :person_id, Person.get_list, :id, > :nom, {:prompt => ''Person?''}) %> > Role: > <%= select(:pp, :role, Person::ROLE_NAMES, {:prompt => ''Role?''}) > %> > </p> > <% end %> > <% end %> > > <% end %> > > I get the expected series of 3 <select>s, but their names are not > automatically indexed. So I get: > > <select id="pp_person_id" name="pp[person_id]"> > <select id="pp_role" name="pp[role] "> > > Rather than this: > > <select id="pp_person_id" > name="project[proj_people_attributes][1][person_id]"> > <select id="pp_role" > name="project[proj_people_attributes][1][role] "> > > (At least I *think* that''s what they should be.) > > Now, you may be saying to yourself "he''s got to call those select > helpers on the pp_form object that the fields_for block yields." If I > do that, I get (e.g.): undefined method `merge'' for :nom:Symbol. So are > there different select helpers for using inside fields_for?Nevermind--got it. I *do* have to call those methods on pp_form, and when I do, I''ve got to leave out that first argument that I''d been using to specify the object whose attribute is to be modified. Which makes sense, since the yeilded form helper thingy is already tied to a particular object. So This works: <% for pp in @project.project_person %> <% fields_for ''project[proj_people_attributes][]'', pp do |pp_form| %> <p> Person: <%= pp_form.collection_select(:person_id, Person.get_list, :id, :nom, {:prompt => ''Choose a person''}) %> Role: <%= pp_form.select(:role, Person::ROLE_NAMES, {:prompt => ''Role?''}) %> </p> <% end %> <% end %> Also--FWIW--those names get generated with empty array brackets, like so: Role: <select id="project_proj_people_attributes__role" name="project[proj_people_attributes][][role]"> </select> I guess they get gathered up into a real array w/real indices at the routing stage? At any rate, I''m off and running again... Cheers, -Roy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---