# Deleting an item with ajax

**URL:** <https://www.ruby-forum.com/t/deleting-an-item-with-ajax/114164>\
**Category:** Ruby\
**Created:** [September 8, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/deleting-an-item-with-ajax/114164 "2007-09-08T23:32:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 8, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/deleting-an-item-with-ajax/114164/1 "2007-09-08T23:32:48Z")

</div>

hi again, I managed to work out how to create a new item via ajax and  
the form\_remote\_tag, however don’t know how to delete an item from the  
table.

here’s the code so far, doesn’t work, keeps on popping up saying null.id  
found (e.g. it doesn’t know which item to destroy).

show.rhtml

 \<%= render :partial=\>'item\_list' %\> 

\_item\_list.rhtml

\<% for item in @list.items %\>

- \<%= item.body %\>  
\<%= link\_to\_remote “delete”, :update =\> “item\_list”,  
:url =\> { :action =\> “destroy”, :id =\> item.id }  
\<%= link\_to\_remote ‘delete’, { :action =\> ‘destroy\_item’, :id =\>  
item.id }, :confirm =\> ‘Are you sure?’, :method =\> :post %\> 
 \<% end %\>

item.rb (model)

class Item \< ActiveRecord::Base  
#validates\_presence\_of :body

belongs\_to :list

acts\_as\_list :scope =\> :list  
end

list.rb (controller)

def destroy\_item  
Item.find(params[:id]).destroy  
render :partial=\>‘list’  
end

any ideas?

think i’m nearly there, just don’t know where i’m going wrong?

appreciate it

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 9, 2007, 11:03am UTC](https://www.ruby-forum.com/t/deleting-an-item-with-ajax/114164/2 "2007-09-09T11:03:31Z")

</div>

John G. wrote:

> hi again, I managed to work out how to create a new item via ajax and  
> the form\_remote\_tag, however don’t know how to delete an item from the  
> table.
> 
> here’s the code so far, doesn’t work, keeps on popping up saying null.id
> 
> \<%= render :partial=\>'item\_list' %\> 
> 
> \_item\_list.rhtml
> 
> \<% for item in @list.items %\>  
> …  
> \<% end %\>
> 
> def destroy\_item  
> Item.find(params[:id]).destroy  
> render :partial=\>‘list’  
> end

> found (e.g. it doesn’t know which item to destroy).  
> …no, the item was destroyed, but when it tried rendering the partial  
> again (via ajax Update) there was no item defined, as yo didn’t add a  
> @list variable, therefore there is no item in the loop, when it is  
> rendered secondly.

add a @list variable to the destroy\_item action and u should be ok.  
btw, better to post on the ruby on rails forum (forum#3 rather than  
forum#4)

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 9, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/deleting-an-item-with-ajax/114164/3 "2007-09-09T13:31:35Z")

</div>

Thanks Shai, I spent a bit last night going through all my rails books  
trying to piece together in my mind how the form\_remote\_tag works,  
learnt a lot.

Understand what your saying and see where I’m going wrong, will try  
changing it.

And yes, i’ll post in the appropriate forum in the future.

Thanks, your a pal.

John.

Shai R. wrote:

> John G. wrote:
> 
> > hi again, I managed to work out how to create a new item via ajax and  
> > the form\_remote\_tag, however don’t know how to delete an item from the  
> > table.
> > 
> > here’s the code so far, doesn’t work, keeps on popping up saying null.id
> > 
> > \<%= render :partial=\>'item\_list' %\> 
> > 
> > \_item\_list.rhtml
> > 
> > \<% for item in @list.items %\>  
> > …  
> > \<% end %\>
> > 
> > def destroy\_item  
> > Item.find(params[:id]).destroy  
> > render :partial=\>‘list’  
> > end
> 
> > found (e.g. it doesn’t know which item to destroy).  
> > …no, the item was destroyed, but when it tried rendering the partial  
> > again (via ajax Update) there was no item defined, as yo didn’t add a  
> > @list variable, therefore there is no item in the loop, when it is  
> > rendered secondly.
> 
> add a @list variable to the destroy\_item action and u should be ok.  
> btw, better to post on the ruby on rails forum (forum#3 rather than  
> forum#4)

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 19, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/deleting-an-item-with-ajax/114164/4 "2007-09-19T23:00:13Z")

</div>

sorted…

def destroy\_item  
Item.find(params[:id]).destroy

```
@list = List.find(params[:list])
render :partial => 'item_list', :object => @list

```

end

\<% for @item in @list.items %\>

- little did i know in rails you can actually send more than one parameter  
on an action.
