Automatically load JS file with View

The techniques for detecting and loading files in this article would apply.

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 (

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 (

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'


hi michal!

Michal Gabrukiewicz [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
    when :action
controller.action_name, true)
      File.join(target, subtarget)

  # We like to keep our application-specific stylesheets and
javascripts in
  # a subdirectory named 'app'.
  skip_base ? path : File.join('app', path)

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")

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")
---- snip ----

and then in your layout:

---- [app/views/layouts/application.rhtml] ----
<%= javascript_include_tag_for :action %>
---- snip ----

this should be quite self-explanatory :wink: