Automatically loading controller-specifc JS

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.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs