# Multiple view types for a single action?

**URL:** <https://www.ruby-forum.com/t/multiple-view-types-for-a-single-action/57148>\
**Category:** Rails\
**Created:** [April 7, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/multiple-view-types-for-a-single-action/57148 "2006-04-07T15:13:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 7, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/multiple-view-types-for-a-single-action/57148/1 "2006-04-07T15:13:37Z")

</div>

Is it possible to have more than one kind of view for a specific action,  
for example an .rhtml and a .rjs file to handle the view for the same  
action?

I suspect not, but this makes me wonder if there a way to call the .rjs  
file from within the .rhtml so the statements within it get executed?

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 7, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/multiple-view-types-for-a-single-action/57148/2 "2006-04-07T15:22:44Z")

</div>

> Is it possible to have more than one kind of view for a specific action,  
> for example an .rhtml and a .rjs file to handle the view for the same  
> action?
> 
> I suspect not, but this makes me wonder if there a way to call the .rjs  
> file from within the .rhtml so the statements within it get executed?

[http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html#M000062](http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html#M000062)

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 7, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/multiple-view-types-for-a-single-action/57148/3 "2006-04-07T16:13:24Z")

</div>

Thank you Rimantas, I am sorry but should have clarified that I wanted  
to be able to use the two views together for the single action, the page  
you pointed me to answers the question the way I posted it but I should  
have elaborated, apologies!

Basically, I want my .rhtml view file to be the display portion for the  
given action but I also want it to be able to trigger the javascrpt  
calls from a corresponding .rjs file, and this I am not quite sure how  
to accomplish.

A quick of summary of what I want to accomplish is having do\_stuff.rhtml  
display the given page for an action ( do\_stuff ) but also have  
do\_stuff.rjs contain javascript commands to show and hide different  
elements on the page, I am not sure how to get to the .rjs file from the  
.rhtml file.

Thanks!  
Andy

Rimantas L. wrote:

> > Is it possible to have more than one kind of view for a specific action,  
> > for example an .rhtml and a .rjs file to handle the view for the same  
> > action?
> > 
> > I suspect not, but this makes me wonder if there a way to call the .rjs  
> > file from within the .rhtml so the statements within it get executed?
> 
> [http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html#M000062](http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html#M000062)
> 
> ## Regards, Rimantas
> 
> [http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 7, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/multiple-view-types-for-a-single-action/57148/4 "2006-04-07T17:04:17Z")

</div>

\<…\>

> A quick of summary of what I want to accomplish is having do\_stuff.rhtml  
> display the given page for an action ( do\_stuff ) but also have  
> do\_stuff.rjs contain javascript commands to show and hide different  
> elements on the page, I am not sure how to get to the .rjs file from the  
> .rhtml file.

You may check render :update

> **[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.

(search for  
“Rendering inline JavaScriptGenerator page updates”)  
and  
[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html)

These shoul let you to achieve the effect you want. If you want to use  
ready made .rjs  
then I have no answer yet, and it would be interesting to find out if  
there is a way to do it.

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 7, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/multiple-view-types-for-a-single-action/57148/5 "2006-04-07T18:05:32Z")

</div>

\<…\>

> try { $(‘stuff1\_div’).show(); $(‘stuff2\_div’).show(); } catch (e) {  
> alert('RJS error …, etc
> 
> Any idea what I did wrong, I know page[‘div\_id\_here’].hide/show works  
> from inside an .rjs template so am confused why it is not working here.

You can use render :update in _controller’s action_ which responds to  
AJAX request.

If you want to have this update code in your normal _view_ use  
update\_page\_tag, like this:

\<%= update\_page\_tag do |page|  
page[‘stuff1\_div’].show  
page[‘stuff2\_div’].show  
end  
%\>

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 7, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/multiple-view-types-for-a-single-action/57148/6 "2006-04-07T17:32:26Z")

</div>

Thats terrific, if I could get that to work it would certainly be an  
acceptable solution, but I get errors →

I have the following div containers in my .rhtml page:

and added this inline javascript using the api as an example:

\<%=  
render :update do |page|  
page[‘stuff1\_div’].show  
page[‘stuff2\_div’].show  
end  
%\>

which is giving me these errors:

try { $(‘stuff1\_div’).show(); $(‘stuff2\_div’).show(); } catch (e) {  
alert('RJS error …, etc

Any idea what I did wrong, I know page[‘div\_id\_here’].hide/show works  
from inside an .rjs template so am confused why it is not working here.

Thanks again!  
Andy

Rimantas L. wrote:

> \<…\>
> 
> > A quick of summary of what I want to accomplish is having do\_stuff.rhtml  
> > display the given page for an action ( do\_stuff ) but also have  
> > do\_stuff.rjs contain javascript commands to show and hide different  
> > elements on the page, I am not sure how to get to the .rjs file from the  
> > .rhtml file.
> 
> You may check render :update  
> [ActionController::Base](http://api.rubyonrails.org/classes/ActionController/Base.html#M000206)  
> (search for  
> “Rendering inline JavaScriptGenerator page updates”)  
> and  
> [http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html)
> 
> These shoul let you to achieve the effect you want. If you want to use  
> ready made .rjs  
> then I have no answer yet, and it would be interesting to find out if  
> there is a way to do it.
> 
> ## Regards, Rimantas
> 
> [http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![aginghipster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@aginghipster](https://www.ruby-forum.com/u/aginghipster)\
**Post date:** [April 7, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/multiple-view-types-for-a-single-action/57148/7 "2006-04-07T18:49:39Z")

</div>

Thats awesome, exactly what I needed to know, thank you very much for  
taking the time to illustrate this stuff! 🙂

-Andy

Rimantas L. wrote:

> \<…\>
> 
> > try { $(‘stuff1\_div’).show(); $(‘stuff2\_div’).show(); } catch (e) {  
> > alert('RJS error …, etc
> > 
> > Any idea what I did wrong, I know page[‘div\_id\_here’].hide/show works  
> > from inside an .rjs template so am confused why it is not working here.
> 
> You can use render :update in _controller’s action_ which responds to  
> AJAX request.
> 
> If you want to have this update code in your normal _view_ use  
> update\_page\_tag, like this:
> 
> \<%= update\_page\_tag do |page|  
> page[‘stuff1\_div’].show  
> page[‘stuff2\_div’].show  
> end  
> %\>
> 
> ## Regards, Rimantas
> 
> [http://rimantas.com/](http://rimantas.com/)
