# Trying to use AJAX to delete a new record

**URL:** <https://www.ruby-forum.com/t/trying-to-use-ajax-to-delete-a-new-record/193302>\
**Category:** Rails\
**Created:** [August 15, 2010, 7:26am UTC](https://www.ruby-forum.com/t/trying-to-use-ajax-to-delete-a-new-record/193302 "2010-08-15T07:26:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 15, 2010, 7:26am UTC](https://www.ruby-forum.com/t/trying-to-use-ajax-to-delete-a-new-record/193302/1 "2010-08-15T07:26:14Z")

</div>

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 %\>

Day
 \<%= 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 %\> 

| Sex | Month | Year |
| --- | --- | --- |
|
| |

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 %\>

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 15, 2010, 4:12pm UTC](https://www.ruby-forum.com/t/trying-to-use-ajax-to-delete-a-new-record/193302/2 "2010-08-15T16:12:36Z")

</div>

Hi Bob,

On Sun, Aug 15, 2010 at 12:26 AM, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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…

At a minimum, you’re going to have to put unique IDs on the person

s to be able to target them with javascript.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 15, 2010, 11:46pm UTC](https://www.ruby-forum.com/t/trying-to-use-ajax-to-delete-a-new-record/193302/3 "2010-08-15T23:46:42Z")

</div>

Bill W. wrote:

> Hi Bob,
> 
> On Sun, Aug 15, 2010 at 12:26 AM, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 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…
> 
> At a minimum, you’re going to have to put unique IDs on the person
> 
> s to be able to target them with javascript.
> 
> HTH,  
> Bill

That is what I was hoping div\_for would do, but I don’t know how to tell  
the button what unique string div\_for invented for this row.

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 16, 2010, 12:33am UTC](https://www.ruby-forum.com/t/trying-to-use-ajax-to-delete-a-new-record/193302/4 "2010-08-16T00:33:03Z")

</div>

You have to supply the id. The ‘easiest’ thing to do would probably  
be to have the destroy or cancel button submit via ajax to the destroy  
method and, if there’s no id, simply re-render your partial to display  
the ‘saved’ household members. If I understand your code correctly,  
the problem you’re having results from trying to do too much via js in  
the new method. I’d recommend a ‘save’ button on new people.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 16, 2010, 5:12pm UTC](https://www.ruby-forum.com/t/trying-to-use-ajax-to-delete-a-new-record/193302/5 "2010-08-16T17:12:14Z")

</div>

Take a look at Railscasts #197, I think it might help you find a  
solution to your problem.
