# Render :partial and DOM manipulation

**URL:** <https://www.ruby-forum.com/t/render-partial-and-dom-manipulation/185099>\
**Category:** Rails\
**Created:** [March 13, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/render-partial-and-dom-manipulation/185099 "2010-03-13T18:02:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [March 13, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/render-partial-and-dom-manipulation/185099/1 "2010-03-13T18:02:31Z")

</div>

Hi guys,

I am stuck with something, I was hoping someone could point me in the  
right direction…

In short:

- I need to render a bunch of partials (corresponding to models)
- I need to do some DOM logic on them (check if they already exist in  
the page, insert them at the right place in the DOM hierarchy, etc)
- I need to do the above first following a standard call, and  
subsequently after AJAX ones.

Right now I have two different methods for each type of call:

- standard: I render the partials inline in the view - it seems fine  
and easy
- AJAX: I am trying to use render :update, but the DOM logic becomes  
really difficult to write (with a lot of page \<\<  
‘javascript\_and\_prototype\_stuff’)

The fact I can’t get the same method to work in both cases probably  
mean I am missing something. Besides, in the AJAX case, the code  
doesn’t look very good at all (mixing js logic and Ruby), and it’s a  
lot more complicated than if I were to write plain javascript.

Is there a recommended/standard way to do this?

And as a side question: if I go for a pure javascript implementation,  
I am going to miss the convenient render :partial method, isn’t it? Or  
is there a way to access it from javascript when it’s rendered?

Thanks a lot,  
Peter

---

<div class="post-metadata">

**Author:** ![PierreW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@PierreW](https://www.ruby-forum.com/u/PierreW)\
**Post date:** [March 14, 2010, 12:57am UTC](https://www.ruby-forum.com/t/render-partial-and-dom-manipulation/185099/2 "2010-03-14T00:57:27Z")

</div>

On Mar 13, 5:01 pm, PierreW [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> subsequently after AJAX ones.  
> doesn’t look very good at all (mixing js logic and Ruby), and it’s a  
> lot more complicated than if I were to write plain javascript.
> 
> Is there a recommended/standard way to do this?
> 
> And as a side question: if I go for a pure javascript implementation,  
> I am going to miss the convenient render :partial method, isn’t it? Or  
> is there a way to access it from javascript when it’s rendered?

You may find things a little easier with a little more actual  
javascript, eg have your action render the same html fragments but  
then use the onSuccess/onComplete callbacks (assuming you are using  
prototype) to sort stuff out clientside (be careful which one you use

- Ajax.Updater uses one of these to insert its content, so if you use  
the wrong one your callback runs before the dom has been updated). I’m  
not entirely sure what you are doing but you could have your ajax  
updates appear in a hidden div and move elements out of there into the  
page.

Fred
