AssetTagHelper

In Rails terms, an asset is something static that you want to include on a web page that isn’t controlled by Rails. These include things like stylesheets, JavaScript libraries, and sometimes images.

When working on a small scale, assets are stored in the public/ directory of your Rails application, but you can put them on a separate server and tell Rails where to find them through ActionController::Base.asset_host. A separate server can speed delivery, let you share assets with other applications, or just reduce the amount of work your Rails application has to do itself.

The methods in AssetTagHelper will assume files are in your public/ directory unless you’ve specified otherwise. Most of them generate HTML tags for you, though a few let you specify ways to generate tags in the future.

The methods you should probably focus on initially include:

auto_discovery_link_tag

Lets you specify the location of an RSS or Atom feed.

image_tag

Returns an HTML img tag for the specified image name.

javascript_include_tag

Returns a script tag for the JavaScript files you identify as parameters. If one of the parameters is :defaults, the application.js file will be included, bringing in the Prototype and Script.aculo.us libraries. You can provide full paths to your scripts, even scripts on other servers, or you can just provide the file’s name. If you’re feeling fancy, you can define groups of styles with register_javascript_expansion, and reference them with symbols.

stylesheet_link_tag ...

Get Learning Rails now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.