Bob Smith
2011-Aug-25 04:47 UTC
Trying to use radio_button with a (possibly new) collection
I am trying to do a list of people in a collection that is shown with a partial. This part works fine. The trouble comes when I put radio buttons with each record for which family member gets the mail sent to that house. If the record for a person is new, the radio button returns ID instead of the id number that doesn''t exist yet. How can I get the id of the record after it is saved and put it in household.hoh ? Here is part of the app. By the way, I''m still using 2.3.9 until these problems are gone. Thanks Bob _people.html.erb <%= link_to_function "Add a Person" do |page| page.insert_html :after, "new", :partial => ''shared/ new_person'', :object => Person.new end %> <table width=60%> <th width=5%><b>HOH</b></th> <th width=20%><b>Last Name</b></th> <th width=30%><b>First Name</b></th> <th width=30%><b>M</b></th> <th width=30%><b>Sex</b></th> <th width=30%><b>Month</b></th> <th width=30%><b>Day</b></th> <th width=30%><b>Year</b></th> <%= render :partial => ''shared/person'', :locals => {:household => @household} , :collection => @household.people.sort_by(&:birthday) %> </div> <div id="new"> </div> </table> _person.html.erb <% fields_for "household[people_attributes][]", person do | person_form| %> <div><tr> <td width=100><%= radio_button "household", "hoh", :id, :class => ''hoh'' %></td> <td><%= person_form.text_field :last_name, :style => ''text-align: left'', :class => ''last_name'', :size => 25, :maxlength => 25, :index => nil, :autocomplete => "off" %></td> <td><%= person_form.text_field :first_name, :style => ''text-align: left'', :class => ''first_name'', :size => 25, :maxlength =>25, :index => nil, :autocomplete => "off" %></td> <td><%= person_form.text_field :middle, :style => ''text-align: right'', :class => ''middle'', :size => 1, :maxlength =>1, :index => nil, :autocomplete => "off" %></td> <td><%= person_form.text_field :sex, :style => ''text-align: right'', :size => 1, :maxlength =>1, :index => nil, :autocomplete => "off" %></td> <td><%= person_form.text_field :month_, :style => ''text-align: right'', :size => 2, :maxlength =>2, :index => nil, :autocomplete => "off" %></td> <td><%= person_form.text_field :day_, :style => ''text-align: right'', :size => 2, :maxlength =>2, :index => nil, :autocomplete => "off" %></td> <td><%= person_form.text_field :year_, :style => ''text-align: right'', :size => 4, :maxlength =>4, :index => nil, :autocomplete => "off" %></td> <td><% if person_form.object.new_record? %> <%= link_to_function "Delete", "delete_row()" %> <% else %> <%= person_form.hidden_field :id, :index => nil %> <%= link_to ''Delete'', person_path(person.id), :confirm => ''Are you sure?'', :method => :delete %> <% end %> household.rb def after_save person = Person.find(self.hoh) # Of course, this is 0 at the moment. I did after_save so the | person record would be there to access, I just need the id of it now, self.first_name = person.first_name self.last_name = person.last_name self.middle = person.middle end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.