[Rails] Trying to use AJAX to delete a new record
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
<b>People in Household</b>
<% @household.build_person unless @household.people %>
<table border=1>
<th width=60 align=left>Sex</th><th width=60 align=left>Month</th><th
width=60 align=left>Day</th><th width=60 align=left>Year</th>
<div id="people">
<%= render :partial => 'person', :collection => @household.people %>
</div>
<%= link_to_function "Add a Person" do |page| page.insert_html :after,
:empty_row, :partial => 'new_person', :object => Person.new end %>
<tr id="empty_row">
</tr>
</table>
person.html.erb
< div id='person'>
<% fields_for "household[people_attributes][]", person do |person_form|
%>
<tr>
<td><%= person_form.text_field :sex, :size => 1, :maxlength =>1,
:index => nil, :autocomplete => "off" %></td>
<td><%= person_form.text_field :month_, :size => 2, :maxlength =>2,
:index => nil, :autocomplete => "off" %></td>
<td><%= person_form.text_field :day_, :size => 2, :maxlength =>2,
:index => nil, :autocomplete => "off" %></td>
<td><%= person_form.text_field :year_, :size => 4, :maxlength =>4,
:index => nil, :autocomplete => "off" %></td>
<% unless person_form.object.new_record? %>
<td>
<%= person_form.hidden_field :id, :index => nil %>
<%= link_to 'Delete', household_person_path(@household, person),
:confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
new_person.html.erb
<% div_for (person, :class => "person") do %>
<% fields_for "household[people_attributes][]", new_person do
|person_form| %>
<tr>
<td><%= person_form.text_field :sex, :size => 1, :maxlength =>1,
:index => nil, :autocomplete => "off" %></td>
<td><%= person_form.text_field :month_, :size => 2, :maxlength =>2,
:index => nil, :autocomplete => "off" %></td>
<td><%= person_form.text_field :day_, :size => 2, :maxlength =>2,
:index => nil, :autocomplete => "off" %></td>
<td><%= person_form.text_field :year_, :size => 4, :maxlength =>4,
:index => nil, :autocomplete => "off" %></td>
<td><%= link_to_function "Delete", "$('new_person').remove();" %></td>
</tr>
<% end %><% end %>
--
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@googlegroups.com.
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home