I have some AJAX in my app which works fine to add new people to a list
shown on-screen. The problem comes if I try to delete one of the new
people. Once saved, there is no problem, as there is an id to delete
with. The problem comes when I try to remove a new person from the
screen. The closest I got was a div around all people and a remove of
that div. All the delete links ( one each person) would delete the first
item on the list…(of course since it only had the div holding
everything to use). I tried div_for and content_tag, but each needed an
id to work with…
Please help… Thanks
some code…
people.html.erb
People in Household
<% @household.build_person unless @household.people %>
| Sex |
Month | DayYear |
<%= render :partial => 'person', :collection => @household.people %>
<%= link_to_function "Add a Person" do |page| page.insert_html :after,
:empty_row, :partial => 'new_person', :object => Person.new end %>
person.html.erb
< div id=‘person’>
<% fields_for “household[people_attributes][]”, person do |person_form|
%>
| <%= person_form.text_field :sex, :size => 1, :maxlength =>1,
:index => nil, :autocomplete => "off" %> |
<%= person_form.text_field :month_, :size => 2, :maxlength =>2,
:index => nil, :autocomplete => "off" %> |
<%= person_form.text_field :day_, :size => 2, :maxlength =>2,
:index => nil, :autocomplete => "off" %> |
<%= person_form.text_field :year_, :size => 4, :maxlength =>4,
:index => nil, :autocomplete => "off" %> |
<% unless person_form.object.new_record? %>
<%= person_form.hidden_field :id, :index => nil %>
<%= link_to 'Delete', household_person_path(@household, person),
:confirm => 'Are you sure?', :method => :delete %> |
<% end %>
<% end %>
new_person.html.erb
<% div_for (person, :class => “person”) do %>
<% fields_for “household[people_attributes][]”, new_person do
|person_form| %>
| <%= person_form.text_field :sex, :size => 1, :maxlength =>1,
:index => nil, :autocomplete => "off" %> |
<%= person_form.text_field :month_, :size => 2, :maxlength =>2,
:index => nil, :autocomplete => "off" %> |
<%= person_form.text_field :day_, :size => 2, :maxlength =>2,
:index => nil, :autocomplete => "off" %> |
<%= person_form.text_field :year_, :size => 4, :maxlength =>4,
:index => nil, :autocomplete => "off" %> |
<%= link_to_function "Delete", "$('new_person').remove();" %> |
<% end %><% end %>