# Automatically load JS file with View

**URL:** <https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299>\
**Category:** Rails\
**Created:** [November 15, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299 "2007-11-15T16:55:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 15, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/1 "2007-11-15T16:55:26Z")

</div>

hi guys, i am quite new to RoR and i was wondering if it is possible to  
achieve an automatic load of a javascript file for a view… it should  
work completely magically 🙂 like with the rjs files…

when i have a view ‘test.rhtml’ and there is a ‘test.js’ in the same  
folder then it should include a script tag for it… does someone has a  
solution or maybe this is already solved somehow within ror.  
thanks in advance!!

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 15, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/2 "2007-11-15T17:16:57Z")

</div>

Michal G. wrote:

> hi guys, i am quite new to RoR and i was wondering if it is possible to  
> achieve an automatic load of a javascript file for a view… it should  
> work completely magically 🙂 like with the rjs files…
> 
> when i have a view ‘test.rhtml’ and there is a ‘test.js’ in the same  
> folder then it should include a script tag for it… does someone has a  
> solution or maybe this is already solved somehow within ror.  
> thanks in advance!!

You could do it this way but the rails looks at the javascript folder  
within the public folder for the scipt:

\<%= javascript\_include\_tag “test” %\>

Or you could do it the normal way of including the in the head, but you  
should keep all your srcipts in the javascript folder, that’s why it is  
there and helps keep things organized. Good luck,

-S

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 15, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/3 "2007-11-15T18:05:42Z")

</div>

thanks … but this is not exactly what i am looking for … the  
javascript i want to include is a specific one for a specific view (so  
each view could have its own) and therefore it make sense to have it in  
the view folder … my question is how i automatically load it …

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 15, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/4 "2007-11-15T19:29:27Z")

</div>

On Nov 15, 2007, at 7:55 AM, Michal G. wrote:

> thanks in advance!!  
> The techniques for detecting and loading files in this article would  
> apply.

> **[WEBSITE.WS - Your Internet Address For Life™](http://www.railsdev.ws/blog/3/modular-page-assembly-in-rails/)**

If the js file name is based on the view name (not the names of  
partials), then you could have a short routine in the Layout which  
uses the controller :action name (which by default is the name of the  
view) to construct the file name you’re looking for and use  
File.exist? to determine whether or not to render a script tag into  
the page head.

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 15, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/5 "2007-11-15T23:16:09Z")

</div>

thanks greg … this approach is good. it works when i add the following  
line

\<%=  
javascript\_include\_tag("/#{controller.controller\_path}/#{controller.action\_name}.js")  
if  
File.exist?("#{RAILS\_ROOT}/app/views/#{controller.controller\_path}/#{controller.action\_name}.js")  
%\>

the problem now is that the file is linked there but the javascript is  
not loaded because its not in the “public” folder … its in the views  
folder …

do you have any idea?

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 16, 2007, 12:04am UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/6 "2007-11-16T00:04:38Z")

</div>

On Nov 15, 2007, at 2:16 PM, Michal G. wrote:

> %\>
> 
> the problem now is that the file is linked there but the javascript is  
> not loaded because its not in the “public” folder .. its in the views  
> folder ..
> 
> do you have any idea?

Ah, sure. Should have seen that coming.

Well, the answer I have is probably very non-Railsy, but it’s what I  
have done with another framework which was organized to do things  
like you’re doing.

Conceptually you have to allow Apache (or whatever) to access the /  
app/views/ folder by changing the permissions.

_HOWEVER_ that has some nasty security implications by allowing the  
source code of files in the views folder to be viewable. To  
counteract that, you can modify the VirtualHost directive in Apache  
to explicitly deny serving specific files by file extension (thus  
allowing .js but denying .rb, .rhtml etc). How that can be done with  
other HTTP servers I don’t know.

I would think that approach would work, but I’m not sure at all what  
other, if any, security implications for Rails in particular would be.

As someone here recently said “Fight Rails, and it fights back” –  
there are areas where I am figuring out how to deliver the last punch  
so I can shove \*my opinion down its throat, but I haven’t done any  
experimenting with this specific one yet.

I’m sure plenty of folks will hate my idea, so maybe let’s wait and  
see if there’s a better, more Railsy, idea?

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 16, 2007, 1:20am UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/7 "2007-11-16T01:20:52Z")

</div>

thanks for that extensive explanation. it did help me to rethink my  
strategy … i am not sure if i want to do all this just to have my JS  
files included. I think i rather keep bunging the script directly into  
the view.  
I wonder how the guys from rails do it… i dont think they put  
everything into the application.js

however, thanks a lot gregor… and its good to see that there are more  
ppl out there starting with this new framework … i will check you blog

- good posts!

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 16, 2007, 2:10am UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/8 "2007-11-16T02:10:15Z")

</div>

There is another alternative:

\<%# application.rhtml.erb %\>

\<%= javascript\_include\_tag(yield :javascripts) if yield(:javascripts) %\>

\<%# my\_random\_view.rhtml.erb %\>

\<% content\_for :javascripts do -%\>some\_other\_javascript\_file\<% end -%\>

That should allow you to set the javascript in the head element where  
it belongs without being too hacky.

Of course, you can do it in the controller with a simple:

@content\_for\_javascripts = ‘some\_other\_javascript\_file’

–s

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 16, 2007, 2:13am UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/9 "2007-11-16T02:13:56Z")

</div>

Steve R. wrote:

> There is another alternative:
> 
> \<%# application.rhtml.erb %\>
> 
> \<%= javascript\_include\_tag(yield :javascripts) if yield(:javascripts) %\>
> 
> \<%# my\_random\_view.rhtml.erb %\>
> 
> \<% content\_for :javascripts do -%\>some\_other\_javascript\_file\<% end -%\>
> 
> That should allow you to set the javascript in the head element where  
> it belongs without being too hacky.
> 
> Of course, you can do it in the controller with a simple:
> 
> @content\_for\_javascripts = ‘some\_other\_javascript\_file’
> 
> –s

thats good but this does not seperates my javascript file from the view  
.. i dont want to put the javascript into the head.. i rather want to  
seperate the javascript into an own file. An the best would be if the  
view/controller can load this file automatically if it is present…

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 16, 2007, 11:48am UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/10 "2007-11-16T11:48:54Z")

</div>

hi michal!

Michal G. [2007-11-16 02:13]:

> thats good but this does not seperates my javascript file from  
> the view .. i dont want to put the javascript into the head.. i  
> rather want to seperate the javascript into an own file. An the  
> best would be if the view/controller can load this file  
> automatically if it is present…  
> i’m sorry, i didn’t follow the whole thread. but we’re doing  
> something similar, which i just refactored to also allow inclusion  
> of action-specific stylesheets and javascripts:

---- [app/helpers/application\_helper.rb] ----  
def asset\_path\_for(target, subtarget = nil, skip\_base = false)  
path = case target  
when :controller  
controller.controller\_name  
when :action  
asset\_path\_for(controller.controller\_name,  
controller.action\_name, true)  
else  
File.join(target, subtarget)  
end

# We like to keep our application-specific stylesheets and

javascripts in

# a subdirectory named ‘app’.

skip\_base ? path : File.join(‘app’, path)  
end

def stylesheet\_link\_tag\_for(target, subtarget = nil)  
path = asset\_path\_for(target, subtarget)

stylesheet\_link\_tag(path)   
if File.exists?(“#{RAILS\_ROOT}/public/stylesheets/#{path}.css”)  
end

def javascript\_include\_tag\_for(target, subtarget = nil)  
path = asset\_path\_for(target, subtarget)

javascript\_include\_tag(path)   
if File.exists?(“#{RAILS\_ROOT}/public/javascripts/#{path}.js”)  
end  
---- snip ----

and then in your layout:

---- [app/views/layouts/application.rhtml] ----  
\<%= javascript\_include\_tag\_for :action %\>  
---- snip ----

this should be quite self-explanatory 😉

cheers  
jens

–  
Jens W., Dipl.-Bibl. (FH)  
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre  
Kunsthistorisches Institut der Universität zu  
KölnAlbertus-Magnus-Platz, D-50923  
KölnTel.: +49 (0)221 470-6668, E-Mail: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.prometheus-bildarchiv.de/](http://www.prometheus-bildarchiv.de/)

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [November 16, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/automatically-load-js-file-with-view/120299/11 "2007-11-16T16:59:09Z")

</div>

thanks jens … as i see you fix the problem by placing the assets in an  
own folder within the public folder. Thats a possible solution … IMHO  
it would be nicer for me to have the right beside the view …  
i will think which approach i will use…  
thanks a lot guys!

michal - [http://webdevbros.com](http://webdevbros.com)
