ActionView::Helpers::AssetUrlHelper

Alias: path_to_asset

def asset_path(source, options = {})

Description

Returns the path to an asset as a string. Asset tag helpers such as image_tag use it to build values like src.

image_path, javascript_path, stylesheet_path, audio_path, video_path and font_path call this helper with their kind as the :type option (image_path("logo.png") is the same as asset_path("logo.png", type: :image)).

Arguments

Changing a value updates the ERB and HTML.

Converted to a path. Depending on the value:
  • A URL (a string starting with https:// or another …://, //, data: or cid:): returned as is.
  • A string starting with / ("/photo.png"): used as the path as is.
  • Any other string ("logo.png"): looks up the asset (a file in app/assets) and uses its path, with a digest added to the file name. If the asset is not found, raises Propshaft::MissingAssetError. The lookup and digest come from Propshaft; in an app without an asset pipeline, it just prepends / (or, if the kind is known, a folder such as /images/) to the path.
Anything from ? or # on (if at least one character follows) is removed while building the path and put back at the end. An empty string returns an empty string; nil raises ArgumentError.
options= {}
The kind of asset. Depending on the value:
  • :javascript or :stylesheet: appends .js or .css unless source already has that extension ("application" looks up application.css; "logo.png" looks up logo.png.css, so it raises Propshaft::MissingAssetError).
  • With skip_pipeline: also prepends the folder for that kind (/images for :image).
  • Otherwise: Propshaft does not sort assets by kind, so the path stays the same (an app without an asset pipeline adds the folder even without skip_pipeline).
".css"
Appended instead of the extension that type gives, unless source already has it. On "logo.png" it looks up logo.png.css, so it raises Propshaft::MissingAssetError. false appends no extension, even when type is :javascript or similar.
true skips the asset lookup and just prepends / to the path (or a folder for the kind, such as /images/, when type is also given). Used for files in public.
"cdn.example.com"
The host prepended to the path. Defaults to config.asset_host. This site leaves it nil, so no host is added unless protocol is :request. A host that does not start with https:// or similar gets the prefix that protocol decides. A %d in it is replaced with a number from 0 to 3, fixed for each path (assets%d.example.com becomes assets0.example.com or similar).
The prefix for a host that does not start with https:// or similar. Depending on the value:
  • :relative: prepends // (//cdn.example.com/…).
  • :request: prepends the current request's protocol (http:// or https://). If neither host nor config.asset_host is set, prepends the current request's host (giving the same URL as asset_url).
  • A string ("https"): prepends it followed by ://.
Defaults to config.action_controller.default_asset_host_protocol, or, if that is not set either, :request when there is a request and :relative when there is none (such as in a mailer).
Any other options are ignored.

ERB

Changes from the previous output: Added Removed Changed


          

HTML