ActionView::Helpers::AssetTagHelper
def image_tag(source, options = {})

Description

Creates an <img>.

Arguments

Changing a value updates the ERB and HTML.

Used as the src attribute. Depending on the value:
  • A URL (a string starting with https:// or another …://, //, data: or cid:): used as is.
  • A string starting with / ("/photo.png"): used 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 /images/ to the path. The extension cannot be left out.
  • Anything other than a string or a symbol (such as a model object): converted to a URL with polymorphic_url.
options= {}
"16x10"
Two numbers joined by x, such as "16x10", are split into the width and height attributes. A single number (16) sets both to the same value. Any other format adds nothing. Raises ArgumentError if width or height is also given.
"32"
Sets the width attribute. Raises ArgumentError if size is also given.
{"logo.png": "1x", "logo@2x.png": "2x"}
A hash or an array sets srcset to pairs of a path and its width or density (1x, 100w), joined with commas. The paths are built the same way as the source argument. A string is used as is.
true skips the asset lookup and prepends /images/ to the path.
"lazy"
Sets the loading attribute. Defaults to config.action_view.image_loading, which is nil on this site, so nothing is added. If the setting has a value, passing nil or false also gives the setting's value.
"async"
Sets the decoding attribute. Defaults to config.action_view.image_decoding, which is nil on this site, so nothing is added.
"other.png"
Ignored. The src attribute is always built from the source argument.
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <img>, and ones this site considers rarely used, are not listed here.

Shared by all helpers

{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
Any other options become HTML attributes of the <img>; those with a nil value are left out.

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview