Provides methods for linking an HTML page together with other assets such as images, javascripts, stylesheets, and feeds. You can direct Rails to link to assets from a dedicated assets server by setting ActionController::Base.asset_host in your environment.rb. These methods do not verify the assets exist before linking to them.

  ActionController::Base.asset_host = "http://assets.example.com"
    => <img src="http://assets.example.com/images/rails.png" alt="Rails" />
    => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="Stylesheet" type="text/css" />


JAVASCRIPT_DEFAULT_SOURCES = ['prototype', 'effects', 'dragdrop', 'controls'] unless const_defined?(:JAVASCRIPT_DEFAULT_SOURCES)


Show files where this module is defined (1 file)
Register or log in to add new notes.
January 27, 2010
1 thank

Alternative hostname generation method

Instead of using a random number to generate the hostname for the single asset, I prefer using source.hash.modulo, so that a given file is always served from the same host. This makes the content more cacheable by browsers and proxies.

ActionController::Base.asset_host = Proc.new { |source|
  "http://assets#{ source.hash.modulo(9) }.example.com"

I didn’t benchmark how long it takes, but String#hash should be reasonably fast.

December 16, 2010 - (>= v2.0.0)
0 thanks

RE: Alternative hostname generation method

According to compute_asset_host as of rails 2.0.0 the %d expansion works in the same way, it’s not a random number generated on each call but the source hash mod 4.

In my brief testing in rails 2.3.9, %d doesn’t appear to work if you use a Proc, so manual generation is of use in that case.