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
image_tag
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
Returns a link
tag for
the CSS stylesheet files you identify as parameters. You can
provide full paths to your stylesheets, even stylesheets on other
servers, or you can just provide the name of the file. The
:all
symbol will link all of
the stylesheets in the public/stylesheets directory. As with
scripts, if you’re feeling really fancy, you can define groups of
styles with register_stylesheet_expansion
, and
reference them with symbols.
There are other methods in AssetTagHelper
, but they’re mostly internal or
only used in special cases:
image_path
(or path_to_image
)An internal method used to calculate where to point for an image.
javascript_path
(or
path_to_javascript
)An internal method used to calculate where to point for a JavaScript file.
register_javascript_expansion
Lets you register a symbol that can reference JavaScript files. Useful if you consistently use a group of script files together.
register_javascript_include_default
Lets you add JavaScript files to the :defaults
symbol used by the javascript_include_tag
method.
register_stylesheet_expansion
Lets you register a symbol that can reference style files. Useful if you consistently use a group of stylesheets together.
stylesheet_path
(or
path_to_stylesheet
)An internal method used to calculate where to point for a stylesheet.