# Automatically loading controller-specifc JS

**URL:** <https://discuss.rubyonrails.org/t/automatically-loading-controller-specifc-js/22496>\
**Category:** rubyonrails-talk\
**Created:** [January 1, 2008, 1:09pm UTC](https://discuss.rubyonrails.org/t/automatically-loading-controller-specifc-js/22496 "2008-01-01T13:09:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phillip\_Koebbe](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@Phillip\_Koebbe](https://discuss.rubyonrails.org/u/Phillip_Koebbe)\
**Post date:** [January 1, 2008, 1:09pm UTC](https://discuss.rubyonrails.org/t/automatically-loading-controller-specifc-js/22496/1 "2008-01-01T13:09:05Z")

</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:

&nbsp;&nbsp;\<%= 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
