ActionView::Helpers::AssetUrlHelper

Alias: url_to_asset

def asset_url(source, options = {})

Description

Returns the URL of an asset as a string.

Calls asset_path with the :protocol option set to :request. Apart from the host, the result is the same as asset_path. image_url, javascript_url, stylesheet_url, audio_url, video_url and font_url call this helper with their kind as the :type option.

Arguments

Changing a value updates the ERB and HTML.

Converted to a path as in asset_path, then a host is prepended. 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, with a host prepended.
  • 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, or the current request's host (on this site, this page's host) if that is not set either. Where there is no request (such as in a mailer), it returns the path without a host unless config.asset_host is set, and passing host raises NoMethodError. A host that does not start with https:// or similar gets the current request's protocol (http:// or https://) prepended. A %d in it is replaced with a number from 0 to 3, fixed for each path.
Ignored. It is always overridden with :request.
Any other options are ignored.

ERB

Changes from the previous output: Added Removed Changed


          

HTML