How to generate asset identifiers in Rails 3 for cache management?

In Rails, when serving static files, you get asset IDadded to the URL, for example

<script src="/javascripts/application.js?1300371955" ...
<link href="/stylesheets/custom.css?1299690788" ...

Thus, the URL of the static file changes if the timestamp of the files changes, and browsers automatically request a new file.

I saw that using helper methods to include static assets --- stylesheet_link_tag, javascript_include_tagand image_tag--- Rails automatically adds timestamps to all links to these files.

How can I implement something similar for other assets that do not have such helper methods
, such as .swffiles?

(This "strategy" has to make the reload have a name, if so, what is called?)

+3
source share
2 answers

The Rails method, which adds a timestamp to assets, is called rails_asset_id and is defined in ActionView :: Helpers :: AssetTagHelper. Although this is a private method, it can be accessed in the helper to create it for your own tags:

def swf_param_tag(path)
  asset_id = rails_asset_id(path)
  "<param name='movie' value='#{path}?#{asset_id}'/>"
end

You need to go along the path because it then calls File.mtime (path) .to_i.to_s on it to get the last modified time on it.

There is another explanation that the asset tag buys you here: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html

+4
source

@Unixmonkey: your solution works very well.

Alternatively there is also swf_fu :

SWF Adobe Flash (, javascripts ..) SWFObject 2.2

.

0

All Articles