# Include JavaScript from view helpers

**URL:** <https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220>\
**Category:** Rails\
**Created:** [January 18, 2007, 10:09am UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220 "2007-01-18T10:09:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![udinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a87d85/32.png) [@udinesh](https://www.ruby-forum.com/u/udinesh)\
**Post date:** [January 18, 2007, 10:09am UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220/1 "2007-01-18T10:09:46Z")

</div>

Hi,

I’m not sure how to do this, can any body please help out ?

In my controller i’m rendering a static file named ‘myfile’ using  
rendering from the method named show ie

def show  
render :file =\> ‘C:/myfile’, :layout =\> true  
end

Everything is fine, i’m able to see the myfiile getting rendered  
properly.

But i want to insert a “JavaScript” dynamically into the rendered page.

Something like this  
\<%= javascript\_include\_tag “UtilitiesOn” %\>

ie. the above JavaScript “UtilitiesOn” is not originally part of the  
‘myfile’  
Hence i want to include the above JS file dynamically either from  
controller or view helper is it possible to do?

Thanks and Regards  
Dinesh

---

<div class="post-metadata">

**Author:** ![udinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a87d85/32.png) [@udinesh](https://www.ruby-forum.com/u/udinesh)\
**Post date:** [January 18, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220/2 "2007-01-18T15:33:55Z")

</div>

Dinesh U. wrote:

> def show  
> render :file =\> ‘C:/myfile’, :layout =\> true  
> end

> Something like this  
> \<%= javascript\_include\_tag “UtilitiesOn” %\>
> 
> Hence i want to include the above JS file dynamically either from  
> controller or view helper is it possible to do?

how about:

1. using several layout files, and every layout includes a different js  
file.
2. using controller to specify wanted layout.

---

<div class="post-metadata">

**Author:** ![udinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a87d85/32.png) [@udinesh](https://www.ruby-forum.com/u/udinesh)\
**Post date:** [January 19, 2007, 6:46am UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220/3 "2007-01-19T06:46:10Z")

</div>

Hi Zhan,

Thanks for the reply.

Yesterday i was trying out the whole day to find out a way to do, but i  
could not do, finally i created a “newLayout” and included this JS. The  
“newLayout” differs from the “MasterLayout” with just this JS inclusion  
in the “newLayout”  
(Its great that your idea is coincidence with the way/workaround that i  
finally made and i’m certainly glad about it)

It worked well…

But the problem in long term perspective is that say if we do some  
chagnes to the “MasterLayout” say added few more links, then we have to  
make sure that we replicate these changes to the “newLayout” also. So at  
anytime we may have to maintain two copies of the “Layout” with one the  
Js inclusion and the other without it. and i guess this is an overhead.

Another workaround is that just before rendering the HTML file from the  
controller, use the File IO to read the HTML file and append the

.and this 

also worked, This way we do not need the “newLayout”. But the problem  
here is every time we do a refresh the above gets  
appended to the HTML file and finally multiple   
entries are found in the HTML file. As we know there is no harm evern  
though when we use multiple same …but it does look  
good though rt? if anybody looks at the HTML file…

But since i could not find a way or know how to do it, i’m currently  
using the “newlayout” workaround. Any other ways ?

Thanks  
Dinesh.

---

<div class="post-metadata">

**Author:** ![udinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a87d85/32.png) [@udinesh](https://www.ruby-forum.com/u/udinesh)\
**Post date:** [January 19, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220/4 "2007-01-19T12:02:14Z")

</div>

Hi Francis,

Thanks for the reply.

Basically i’m rendering a “file” like this.

render :file =\> filename, :layout =\> “newLayout”

I tried to use partials, but no luck.

I guess we cannot use the :file =\> filename when we use :partial

Dinesh

Alan F. wrote:

> Dinesh U. wrote:
> 
> > But since i could not find a way or know how to do it, i’m currently  
> > using the “newlayout” workaround. Any other ways ?
> 
> Could you use the same layout file but add a conditional include of a  
> partial ? (said partial just containing the javascript\_include tag).
> 
> A.

---

<div class="post-metadata">

**Author:** ![udinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a87d85/32.png) [@udinesh](https://www.ruby-forum.com/u/udinesh)\
**Post date:** [January 19, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220/5 "2007-01-19T12:56:43Z")

</div>

Dinesh U. wrote:

> Hi Francis,
> 
> Thanks for the reply.
> 
> Basically i’m rendering a “file” like this.
> 
> render :file =\> filename, :layout =\> “newLayout”
> 
> I tried to use partials, but no luck.
> 
> I guess we cannot use the :file =\> filename when we use :partial
> 
> Dinesh

In your newLayout.rhtml file you can have:

\<%= render :partial =\> ‘common/javascript\_1’ %\>

which will render whatever is in the file  
app/views/common/\_javascript\_1.rhtml.  
This could happily be javascript.

---

<div class="post-metadata">

**Author:** ![udinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a87d85/32.png) [@udinesh](https://www.ruby-forum.com/u/udinesh)\
**Post date:** [January 19, 2007, 11:02am UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220/6 "2007-01-19T11:02:42Z")

</div>

Dinesh U. wrote:

> But since i could not find a way or know how to do it, i’m currently  
> using the “newlayout” workaround. Any other ways ?

Could you use the same layout file but add a conditional include of a  
partial ? (said partial just containing the javascript\_include tag).

A.

---

<div class="post-metadata">

**Author:** ![udinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a87d85/32.png) [@udinesh](https://www.ruby-forum.com/u/udinesh)\
**Post date:** [January 22, 2007, 6:58am UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220/7 "2007-01-22T06:58:43Z")

</div>

Hi askegg,

I agree with you that when we do like this  
\<%= render :partial =\> ‘common/javascript\_1’ %\>

which will render whatever is in the file  
app/views/common/\_javascript\_1.rhtml.

But my requirement, the above ‘javaScript’ file has to be  
appended(integrated) to the file which i’m rendering.

As shown in the below example, ‘filename’ is the actual file which i  
want to render, Add-on to that i also want to dynamically include a  
“JavaScript” when the ‘filename’ is rendered. In other words the  
‘filename’ is a static file and the ‘JavaScript’ will not be included in  
that ‘filename’.

I want to include the ‘JavaScript’ from the controller into the  
‘filename’ when it gets rendered from the browser. ie This “JavaScript”  
is NOT part of the initial static ‘filename’. From the Controller based  
on some conditions i want to include different JS into the static file  
name, so that finally the filename and JS is integrated and rendered in  
the browser.

Thanks a LOT  
Dinesh

---

<div class="post-metadata">

**Author:** ![udinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a87d85/32.png) [@udinesh](https://www.ruby-forum.com/u/udinesh)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220/8 "2007-01-19T16:30:35Z")

</div>

On 1/19/07, Dinesh U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> finally made and i’m certainly glad about it)  
> Another workaround is that just before rendering the HTML file from the  
> But since i could not find a way or know how to do it, i’m currently
> 
> >

I have written a plugin that may help you. I’m not sure that I  
completely understand what you want to do though. But it may help.

[http://www.agilewebdevelopment.com/plugins/resource\_on\_demand](http://www.agilewebdevelopment.com/plugins/resource_on_demand)

---

<div class="post-metadata">

**Author:** ![udinesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a87d85/32.png) [@udinesh](https://www.ruby-forum.com/u/udinesh)\
**Post date:** [January 22, 2007, 10:52am UTC](https://www.ruby-forum.com/t/include-javascript-from-view-helpers/82220/9 "2007-01-22T10:52:33Z")

</div>

> From what I read of the API this is not possible as “render :file” is  
> treated like “render :action”, but without the layout applied.

Perhaps you could create a instance variable to read the file into,  
then append the javascript to it? I have not looked at file handling  
in Ruby yet, but it seems easy enough.
