Include JavaScript from view helpers

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

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.

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.

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.

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.

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.

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

On 1/19/07, Dinesh U. [email protected] 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

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.