# RJS conditional element replacement

**URL:** <https://www.ruby-forum.com/t/rjs-conditional-element-replacement/135613>\
**Category:** Rails\
**Created:** [April 23, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/rjs-conditional-element-replacement/135613 "2008-04-23T16:45:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 23, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/rjs-conditional-element-replacement/135613/1 "2008-04-23T16:45:40Z")

</div>

Hi all,

I have an RJS problem. I’m trying to write a helper method that  
replaces a given div element with a given partial, if and only if that  
div exists. I can do this with something like:

page \<\< “if($(‘my\_div’)){”  
page[my\_partial].replace :partial =\> ‘my\_partial’  
page \<\< “}”

The problem with this (apart from it being ugly code) is that the  
partial my\_partial is rendered even if the div element my\_div doesn’t  
exist. In my app, some of the partials I want to use here are pretty  
complex, and rendering them unnecessarily causes a pretty serious  
slowdown. Does anyone know of a way to solve this problem?

Many thanks

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 24, 2008, 1:15pm UTC](https://www.ruby-forum.com/t/rjs-conditional-element-replacement/135613/2 "2008-04-24T13:15:23Z")

</div>

Thanks Fred, that is a good point! There’s a lot of places that I want  
to do this, so I’d want to automate the process of specifying what  
elements are present. Perhaps it would be possible to use an  
Ajax.Responder method to collect a list of divs that are present on  
the page and send it with each Ajax request? I’m not really sure how  
to do this, and how to access the JS variables from Rails (as it’s the  
RJS script which I want to prevent from rendering templates). I may be  
overcomplicating the issue… any thoughts?

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 23, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/rjs-conditional-element-replacement/135613/3 "2008-04-23T17:17:46Z")

</div>

On 23 Apr 2008, at 15:45, Adam wrote:

> The problem with this (apart from it being ugly code) is that the  
> partial my\_partial is rendered even if the div element my\_div doesn’t  
> exist. In my app, some of the partials I want to use here are pretty  
> complex, and rendering them unnecessarily causes a pretty serious  
> slowdown. Does anyone know of a way to solve this problem?

When the client makes the ajax request, it could specify whether or  
not it has it. Fundamentally the server can’t know that sort of stuff  
unless told.

Fred

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 24, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/rjs-conditional-element-replacement/135613/4 "2008-04-24T13:37:14Z")

</div>

On 24 Apr 2008, at 12:14, Adam wrote:

> Thanks Fred, that is a good point! There’s a lot of places that I want  
> to do this, so I’d want to automate the process of specifying what  
> elements are present. Perhaps it would be possible to use an  
> Ajax.Responder method to collect a list of divs that are present on  
> the page and send it with each Ajax request? I’m not really sure how  
> to do this, and how to access the JS variables from Rails (as it’s the  
> RJS script which I want to prevent from rendering templates). I may be  
> overcomplicating the issue… any thoughts?

The with option to the various remote helpers might be useful, so

link\_to\_remote ‘Foo’, :url =\> {…}, :with =\> “{foo: foo, bar: bar}”  
will submit the javascript parameters foo and bar. You can of course  
have pretty much any javascript in there.  
Collecting all the divs on the page would probably be over the top  
though, i’d trip it down to  
link\_to\_remote ‘Foo’, :url =\> {…}, :with =\> “{needsRefresh: !$  
(‘foo’)}”, so needsRefresh will be false if $(‘foo’) exists

Fred

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [April 24, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/rjs-conditional-element-replacement/135613/5 "2008-04-24T15:51:42Z")

</div>

Thanks Fred, that worked a treat 🙂

Interestingly, when I used the syntax you suggested ("{needsRefresh: !$  
(‘foo’)}"), the needsRefresh variable wasn’t available to the RJS  
script, at least not in the params hash. Instead I pieced together a  
string argument of the form “‘foo=’+$(‘foo’)+’&bar=’+$(‘bar’)” and  
passed this to the :with option. This was then available in the params  
hash.

Again, thanks for your help!
