# Link\_to\_remote can't replace specified element?

**URL:** <https://www.ruby-forum.com/t/link-to-remote-cant-replace-specified-element/156625>\
**Category:** Rails\
**Created:** [December 31, 2008, 3:52am UTC](https://www.ruby-forum.com/t/link-to-remote-cant-replace-specified-element/156625 "2008-12-31T03:52:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JHuizingh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@JHuizingh](https://www.ruby-forum.com/u/JHuizingh)\
**Post date:** [December 31, 2008, 3:52am UTC](https://www.ruby-forum.com/t/link-to-remote-cant-replace-specified-element/156625/1 "2008-12-31T03:52:01Z")

</div>

I’m having an issue with the design of my views that I can’t seem to  
wrap my head around. I have a page where I am showing a collection of  
objects in a list. The way that I designed it, it looked something  
like this:

main view:

## here is the list

 \<%= render(:partial =\> "items/item", :collection =\> @items %\>

collection item’s view:

- ... INFO ABOUT item....

This works great, creating a

 that is full of - 's. I hit a  
snag when I start trying to update individual 
- items with ajax  
calls. The :update option for link\_to\_remote() and related functions  
doesn’t give me an option to replace the dom element that it refers to  
as far as I can tell. It lets me specify a dom id that I can change  
the innerHTML of, insert the returned html with in it at the beginning  
or the end, before or after the element, but it doesn’t seem to allow  
me to REPLACE the element.

So a call like this:

remote\_function(:url =\> credential\_search\_instance\_path  
(credential\_search\_instance),  
:method =\> :put,  
:update =\> dom\_id(item)  
)

Will replace the html inside of the

- element, so it would update  
the page to look like this: 
- 
- ... INFO ABOUT item....

I ended up getting around the issue by checking if it is an xhr  
request in my item view like this;  
\<% if ! request.xhr? then %\>

- \<% end %\> ... INFO ABOUT item.... \<% if ! request.xhr? then %\>
\<% end %\>

That solution works, but it seems very inelegant and adds a lot of  
noise to the view. Is it really true that I can’t replace the dom  
element? Am I missing something? Is there a better way to design my  
view and partial?

Thanks,  
Jonathan

---

<div class="post-metadata">

**Author:** ![JHuizingh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@JHuizingh](https://www.ruby-forum.com/u/JHuizingh)\
**Post date:** [January 2, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/link-to-remote-cant-replace-specified-element/156625/2 "2009-01-02T12:07:41Z")

</div>

JHuizingh wrote:

> snag when I start trying to update individual - items with ajax (credential\_search\_instance),
> \<% end %\>
> 
> That solution works, but it seems very inelegant and adds a lot of  
> noise to the view. Is it really true that I can’t replace the dom  
> element? Am I missing something? Is there a better way to design my

The best solution would be to move the li tags to a layout:

render :partial =\> “items/item”, :collection =\> @items, :layout =\> ‘li’

Where \_li.html.erb contains

- \<%= yield %\>

However this is broken in 2.2.2 (it instead renders a collection  
of the collection). I’ve created a ticket:  
[http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1688](http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1688)

So until this works, the other options are:

1. Replace the collection with a loop, either using the  
layout option, or putting the li tags in the main view,

2. Create a second level of partial, or

3. Remove the :update option and use outer-HTML replace RJS calls.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
