Forum: Ruby on Rails [ANN] RequireResourceHelper for JS and CSS

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Duane J. (Guest)
on 2006-01-03 23:03
(Received via mailing list)
The following was posted on my blog at

A friend of mine recently expressed interest in this code snippet, so
I thought I'd send it out to anyone else who's interested.  This
module makes it a lot easier to 'require' javascript and cascading
stylesheet dependencies inside your views or controllers.  By
'convention', the code will automatically include any javascript or
CSS files whose name matches the current controller's name.  In
addition, any file named like 'controller_name_action_name.css' will
also be included for specific actions (likewise for JS).  It was
inspired by a portion of code I saw from Demetrius Nunes.

To use it, put the following in your application.rb:

	class ApplicationController < ActionController::Base
	  helper 'require_resource'

Then in my layout's HTML "head" section:

	<%= stylesheet_auto_link_tags %>
	<%= javascript_auto_include_tags %>

Supposing I am writing an action for WelcomeController called
'index.rhtml', (and that each of the files actually exists), I can
now assume that this HTML will be present in the layout:

	<script src="/javascripts/welcome.js" type="text/javascript"></script>
	<script src="/javascripts/welcome_index.js" type="text/javascript"></
	<link href="/stylesheets/welcome.css" media="screen"
rel="Stylesheet" type="text/css" />
	<link href="/stylesheets/welcome_index.css" media="screen"
rel="Stylesheet" type="text/css" />

If the files don't exist, the above HTML won't be included.

In the case that the CSS or JS is not named 'by convention' as a file
that corresponds with the controller's name, I use the
"require_javascript" code.  In one of my views (index.rhtml, for

	<% require_javascript 'popitup' %>

Then, I can call the js function popitup later:
<%= link_to_function "click me", "popitup('#{url_for :action =>
'click'}')" %>

module RequireResourceHelper
   # Adds resources such as stylesheet or javascript files to the
corresponding array of
   # resources that will be 'required' by the layout. The
+resource_type+ is either
   # :javascript or :stylesheet. The +extension+ is optional, and
should normally correspond
   # with the resource type, e.g. 'css' for :stylesheet and 'js'
for :javascript.
   def autorequire(resource_type, extension = nil)
     extensions = {:stylesheet => 'css', :javascript => 'js'}
     path = "#{RAILS_ROOT}/public/#{resource_type.to_s.pluralize}/"
     candidates = [ "#{controller.controller_name}",
{controller.action_name}" ]

     for candidate in candidates
       if FileTest.exist?("#{path}/#{candidate}.#{extension ||
         require_resource(resource_type, candidate)

   def stylesheet_auto_link_tags
     @stylesheets.uniq.inject("") do |buffer, css|
       buffer << stylesheet_link_tag(css) + "\n    "

   def javascript_auto_include_tags
     @javascripts.uniq.inject("") do |buffer, js|
       buffer << javascript_include_tag(js) + "\n    "

   def require_resource(type, name)
     variable = type.to_s.pluralize
     array = instance_variable_get("@#{variable}")
     instance_variable_set("@#{variable}", (array || []) | [name])

   def require_javascript(script)
     require_resource(:javascript, script)

   def require_stylesheet(sheet)
     require_resource(:stylesheet, sheet)

Duane J.
Ramin B. (Guest)
on 2006-01-04 01:23
(Received via mailing list)
Pretty cool!

I wrote something similar to this, where it would just check for the
existance of a resouce before including it. but this is more
This topic is locked and can not be replied to.