# How do I override the default RJS template?

**URL:** <https://www.ruby-forum.com/t/how-do-i-override-the-default-rjs-template/70764>\
**Category:** Rails\
**Created:** [October 2, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/how-do-i-override-the-default-rjs-template/70764 "2006-10-02T20:12:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [October 2, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/how-do-i-override-the-default-rjs-template/70764/1 "2006-10-02T20:12:38Z")

</div>

Hello,

I have an action in my controller called ‘reply’. I would like to be  
able to select a particular RJS file to excute based on the results of  
some logic.

In the simplest terms, this is what I’d like to accomplish:

def reply  
if case == a  
[use reply\_a.rjs]  
else  
[use reply\_b.rjs]  
end  
end

Now Rails has a convention that says, "if no render statement is  
defined, look for and .rhtml or .rjs file to execute based on the name  
of the action; i.e reply.rjs. I can seem to find a particluar command  
that will allow me to specify which RJS to use. It seems that such a  
command should exist, but I am to bone-headed to find it.

(Also, just curious, does anyone know of a way to do logic inside an RJS  
template?)

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [October 3, 2006, 8:45am UTC](https://www.ruby-forum.com/t/how-do-i-override-the-default-rjs-template/70764/2 "2006-10-03T08:45:46Z")

</div>

> You need to use the ‘render’ method, and in particular the :action  
> option:
> 
> render(:action =\> :reply\_a)
> 
> or
> 
> render(:action =\> :reply\_b)
> 
> The documentation’s here:
> 
> [ActionController::Base](http://api.rubyonrails.org/classes/ActionController/Base.html#M000206)
> 
> > (Also, just curious, does anyone know of a way to do logic inside an RJS  
> > template?)
> 
> An RJS template is just pure Ruby, so you can use the usual  
> if/elsif/end etc.
> 
> Chris

Thanks, well done. I had tried, render :parial and render :template. I  
didn’t think of render :action for some reason.

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [October 3, 2006, 12:16am UTC](https://www.ruby-forum.com/t/how-do-i-override-the-default-rjs-template/70764/3 "2006-10-03T00:16:17Z")

</div>

Elliott B. wrote:

> ```
> [use reply_b.rjs]
> 
> ```
> 
> end  
> end
> 
> Now Rails has a convention that says, "if no render statement is  
> defined, look for and .rhtml or .rjs file to execute based on the name  
> of the action; i.e reply.rjs. I can seem to find a particluar command  
> that will allow me to specify which RJS to use.

The clue is in “if no render statement is defined…” 😉

You need to use the ‘render’ method, and in particular the :action  
option:

render(:action =\> :reply\_a)

or

render(:action =\> :reply\_b)

The documentation’s here:

> **[ActionController::Base](https://api.rubyonrails.org/classes/ActionController/Base.html#M000206)**
>
> Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either it renders a template or redirects to another action.

> (Also, just curious, does anyone know of a way to do logic inside an RJS  
> template?)

An RJS template is just pure Ruby, so you can use the usual  
if/elsif/end etc.

Chris

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [October 3, 2006, 11:06am UTC](https://www.ruby-forum.com/t/how-do-i-override-the-default-rjs-template/70764/4 "2006-10-03T11:06:52Z")

</div>

Elliott B. wrote:

> Thanks, well done. I had tried, render :parial and render :template. I  
> didn’t think of render :action for some reason.

Yeah, the :action option is a bit weirdly named. It makes you think  
that it’s going to execute the action you specify, but actually it just  
renders the template by that name.

When I write render(:action =\> :wibble), I like to think of it as  
telling Rails to “pretend that I’m in an action called ‘wibble’, and go  
and find the default template for that action, following the normal  
default rules”. That’s why it works equally well for RHTML/RJS/whatever  
templates without specifying the extension, because Rails’ default  
behaviour is to find one of those templates given just the name of the  
action.

Chris

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [October 3, 2006, 12:07pm UTC](https://www.ruby-forum.com/t/how-do-i-override-the-default-rjs-template/70764/5 "2006-10-03T12:07:56Z")

</div>

> > (Also, just curious, does anyone know of a way to do logic inside an RJS  
> > template?)
> 
> An RJS template is just pure Ruby, so you can use the usual  
> if/elsif/end etc.

…the if/else/etc/ works, but how do you pass parameteres to the RJS  
template? all of the global variables that are in the action are passed  
to the template? (@user, @page, @var) ? i can’t seem to get a variable  
into the RJS template …

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [October 3, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/how-do-i-override-the-default-rjs-template/70764/6 "2006-10-03T14:31:35Z")

</div>

harp wrote:

> how do you pass parameteres to the RJS  
> template? all of the global variables that are in the action are passed  
> to the template? (@user, @page, @var) ? i can’t seem to get a variable  
> into the RJS template …

Yeah, that should work. If you do

@user = User.find(:first)

in your controller, your RJS template should be able to access that  
instance variable as well:

page.alert(@user.name)

Same as any template. Can you post your code that’s not working as  
expected?

Chris
