# Automatically loading controller-specifc JS

**URL:** <https://www.ruby-forum.com/t/automatically-loading-controller-specifc-js/124708>\
**Category:** Rails\
**Created:** [January 1, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/automatically-loading-controller-specifc-js/124708 "2008-01-01T14:09:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [January 1, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/automatically-loading-controller-specifc-js/124708/1 "2008-01-01T14:09:56Z")

</div>

There was a thread recently that brought up the idea of modifying  
javascript\_include\_tag to load a controller-specific js file  
automatically. I took a look at doing that last night, and  
discovered that it’s actually quite a simple thing to do without  
extending the Rails source. In a layout, all you have to do is:

\<%= javascript\_include\_tag params[:controller] if File.exists?("#  
{RAILS\_ROOT}/public/javascripts/#{params[:controller]}.js") %\>

Admittedly, this is not DRY because you would need to do this for  
every application you write. But it is DRY for the current application.

Just thought I’d pass that along.

Peace,  
Phillip
