# Calling one RJS template from another

**URL:** <https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526>\
**Category:** Rails\
**Created:** [September 21, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526 "2006-09-21T16:28:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [September 21, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526/1 "2006-09-21T16:28:04Z")

</div>

I need to call an RJS template from within another template…is there a  
way  
to do this?

Thank you!

Jake

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [September 21, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526/2 "2006-09-21T17:23:27Z")

</div>

Jake C. wrote:

> I need to call an RJS template from within another template…is there a  
> way  
> to do this?
> 
> Thank you!
> 
> Jake

I don’t know of a way. Perhaps if you give us some context we can come  
up with a different way to solve the problem you are working on.  
jp

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [September 21, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526/3 "2006-09-21T20:16:56Z")

</div>

On 9/21/06, Jeff P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t know of a way. Perhaps if you give us some context we can come  
> up with a different way to solve the problem you are working on.

Ok, say I have update\_a.rjs and update\_b.rjs. update\_b.rjs has a lot of  
complex update logic, etc, that is used by various actions across my  
code.  
update\_a.rjs in different places as well.

update\_a.rjs needs to always ensure update\_b.rjs is called directly  
after  
it. Rather that going around making sure every place that update\_a.rjs  
is  
rendered also consequently renders update\_b.rjs, I was hoping I could  
simply  
chain them in some way…

Thanks,  
Jake

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [September 21, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526/4 "2006-09-21T20:53:01Z")

</div>

Jake C. wrote:

> X-Google-AttachSize: 996
> 
> On 9/21/06, **Jeff P.** \<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:
> 
> > I don’t know of a way.&nbsp;&nbsp;Perhaps if you give us some context we can come  
> > up with a different way to solve the problem you are working on.
> 
>   
>   
> Ok, say I have update\_a.rjs and update\_b.rjs. update\_b.rjs has a lot of complex update logic, etc, that is used by various actions across my code. update\_a.rjs in different places as well.  
>   
>   
> update\_a.rjs needs to always ensure update\_b.rjs is called directly after it. Rather that going around making sure every place that update\_a.rjs is rendered also consequently renders update\_b.rjs, I was hoping I could simply chain them in some way…  
>   
>   
> Thanks,  
> Jake  
> 
>   
> 
> ------=\_Part\_8415\_15296355.1158862352948–

You could try something like this…

def rjs\_a  
h = headers[‘Content-Type’]  
render :update do |page|  
page \<\< self.send(“rjs\_b”)  
@performed\_render = false  
headers[‘Content-Type’] = h  
end  
end

Untested, but some variation on this should work.  
The @performed render call will prevent you from getting double render  
errors  
The header magic is sometimes required to prevent your browser from  
just rendering the response as text. You may not need it here.

\_Kevin

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [September 21, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526/5 "2006-09-21T21:15:11Z")

</div>

I ran into this problem about a month ago. I couldn’t find anyone else  
who had found a solution, so I rolled my own. This is a little bit  
messy, but I’m using in a couple place and can tell you for sure that  
it works. In the RJS file called first I use page.call to issue a  
second ajax call. Here is the rjs command I’ve been using:

page.call(“new Function(“new  
Ajax.Request(’/controller/action?qrystring=” +params[:q]+”’,  
{asynchronous:true, evalScripts:true})")")

Hop this helps.

Regards,  
Aaron

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [September 21, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526/6 "2006-09-21T22:50:22Z")

</div>

There at railsconf europe about reusing bits of rjs.  
In a nutshell a reasonable way of doing things is via helper functions,  
a little like so (recalling this from memory):

def common\_fragment  
update\_page do |page|  
page.whatever  
end  
end

Then in your rjs files do  
page\<\<common\_fragment()

Fred

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [September 21, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526/7 "2006-09-21T22:55:53Z")

</div>

Fred wrote:

> There at railsconf europe about reusing bits of rjs.  
> That should of course say ‘There was a talk at railsconf europe’ (by  
> Marcel Molina Jr. slides from talk available at  
> [http://project.ioni.st/images/uploads/EuroRailsConf.pdf](http://project.ioni.st/images/uploads/EuroRailsConf.pdf))

Fred

---

<div class="post-metadata">

**Author:** ![Jake\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@Jake\_C](https://www.ruby-forum.com/u/Jake_C)\
**Post date:** [September 23, 2006, 3:20am UTC](https://www.ruby-forum.com/t/calling-one-rjs-template-from-another/69526/8 "2006-09-23T03:20:48Z")

</div>

On 9/21/06, Fred [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> Then in your rjs files do  
> page\<\<common\_fragment()

Thanks for all the good suggestions guys. I liked them all, but the  
above  
appears to be the cleanest.

Thanks again!  
Jake
