# Deleting

**URL:** <https://www.ruby-forum.com/t/deleting/108383>\
**Category:** Rails\
**Created:** [July 2, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/deleting/108383 "2007-07-02T23:17:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 2, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/deleting/108383/1 "2007-07-02T23:17:43Z")

</div>

I have been try to make a page where a user can add something to a list  
and then delete something from the same list all without re-loading the  
page. The adding part is easy, but the deleting part is turning out to  
be a pain.

A user by the name of Philip gave me some very helpful hints:  
Put the snip of rHTML that needs to refresh into a partial. Include it  
the  
normal way, like this:

 \<%= render :partial =\> 'my\_partial' %\> 

Now wire link\_to\_remote up to an action that looks like this:

def my\_action  
return unless request.xhr?  
render :update do |page| # ← I call that rjs sometimes!  
page.replace\_html ‘refresh\_me’, :partial =\> ‘my\_partial’  
end  
end

The deal is that almost\* anything you can pass to render, you can also  
pass  
to the second argument to JavaScriptGenerator#replace\_html.

The first code injects raw HTML into your page as it renders, before it  
goes  
over the wire. The second snip renders the partial, then creates  
JavaScript  
containing Element.update(‘refresh\_me’, ‘’). This  
goes over the wire, and the Ajax handlers from prototype.js will replace  
the  
innerHTML member of that

 with the new version.

(Question for the lifers - is it _really_ “almost anything”? Or is it  
“anything”!?)

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

This does not work, however, it just reloads the entire page within the  
list. Does anyone have any other ideas or sugestions. I realize that I  
am probably implementing the above code wrong, but I have tried every  
way that I can think of. Thanks,

~s

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 2, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/deleting/108383/2 "2007-07-02T23:36:21Z")

</div>

> ```
> <%= render :partial => 'my_partial' %>
> 
> ```
> 
> (Question for the lifers - is it _really_ “almost anything”? Or is it  
> “anything”!?)

I haven’t come across anything that I couldn’t send back yet…

> This does not work, however, it just reloads the entire page within the  
> list. Does anyone have any other ideas or sugestions. I realize that I  
> am probably implementing the above code wrong, but I have tried every  
> way that I can think of. Thanks,

What does your link\_to\_remote look like? It should just update the  
‘refresh\_me’ div with the result of the partial ‘my\_partial’.

Also, if you want to delete an item, another idea would be to assign  
each  
item it’s own id. Say

This is item 123
...

Then in your delete action (or link to method for that action in a  
callback on success) use an event to simply remove that element from the  
page. Then nothing has to update, and the item simply goes away after  
deleting it.

-philip

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 3, 2007, 12:01am UTC](https://www.ruby-forum.com/t/deleting/108383/3 "2007-07-03T00:01:25Z")

</div>

> It looks like this. ‘hotel\_info-refresh’ is the name of the div and  
> ‘delete\_hotel\_info’ is the name of the method that does the destroy. I  
> will try giving each item an id.  
> \<%= link\_to\_remote ‘Delete’, :update =\> ‘hotel\_info\_refresh’, :action =\>  
> ‘delete\_hotel\_info’, :id =\> @user.id %\>

Did you just mis-type the dash vs underscore in your div id and the  
value  
of :update? Otherwise it looks okay to me.

Oh Wait!

If you’re passing in :update, you don’t need to do a page.replace\_html  
in  
your action. Just render the partial with :layout =\> false.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 2, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/deleting/108383/4 "2007-07-02T23:45:41Z")

</div>

It looks like this. ‘hotel\_info-refresh’ is the name of the div and  
‘delete\_hotel\_info’ is the name of the method that does the destroy. I  
will try giving each item an id.  
\<%= link\_to\_remote ‘Delete’, :update =\> ‘hotel\_info\_refresh’, :action =\>  
‘delete\_hotel\_info’, :id =\> @user.id %\>

Thanks again, for your help.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 3, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/deleting/108383/5 "2007-07-03T20:50:11Z")

</div>

I had to place the :action is side the :url as well as the the  
parameters that I was passing. After I did that everything worked fine.

link\_to\_remote ‘Delete’, :url =\> { :action =\> ‘delete\_hotel\_info’, :id  
=\> @user.id, :hotel =\> @hotel.id }, :update =\> ‘hotel\_info\_refresh’

Thanks Philip
