Method: ActionView::Helpers::AssetTagHelper#picture_tag
- Defined in:
- actionview/lib/action_view/helpers/asset_tag_helper.rb
#picture_tag(*sources, &block) ⇒ Object
Returns an HTML picture tag for the sources. If sources is a string, a single picture tag will be returned. If sources is an array, a picture tag with nested source tags for each source will be returned. The sources can be full paths, files that exist in your public images directory, or Active Storage attachments. Since the picture tag requires an img tag, the last element you provide will be used for the img tag. For complete control over the picture tag, a block can be passed, which will populate the contents of the tag accordingly.
Options
When the last parameter is a hash you can add HTML attributes using that parameter. Apart from all the HTML supported options, the following are supported:
-
:image- Hash of options that are passed directly to theimage_taghelper.
Examples
picture_tag("picture.webp")
# => <picture><img src="/images/picture.webp" /></picture>
picture_tag("gold.png", :image => { :size => "20" })
# => <picture><img height="20" src="/images/gold.png" width="20" /></picture>
picture_tag("gold.png", :image => { :size => "45x70" })
# => <picture><img height="70" src="/images/gold.png" width="45" /></picture>
picture_tag("picture.webp", "picture.png")
# => <picture><source srcset="/images/picture.webp" /><source srcset="/images/picture.png" /><img src="/images/picture.png" /></picture>
picture_tag("picture.webp", "picture.png", :image => { alt: "Image" })
# => <picture><source srcset="/images/picture.webp" /><source srcset="/images/picture.png" /><img alt="Image" src="/images/picture.png" /></picture>
picture_tag(["picture.webp", "picture.png"], :image => { alt: "Image" })
# => <picture><source srcset="/images/picture.webp" /><source srcset="/images/picture.png" /><img alt="Image" src="/images/picture.png" /></picture>
picture_tag(:class => "my-class") { tag(:source, :srcset => image_path("picture.webp")) + image_tag("picture.png", :alt => "Image") }
# => <picture class="my-class"><source srcset="/images/picture.webp" /><img alt="Image" src="/images/picture.png" /></picture>
picture_tag { tag(:source, :srcset => image_path("picture-small.webp"), :media => "(min-width: 600px)") + tag(:source, :srcset => image_path("picture-big.webp")) + image_tag("picture.png", :alt => "Image") }
# => <picture><source srcset="/images/picture-small.webp" media="(min-width: 600px)" /><source srcset="/images/picture-big.webp" /><img alt="Image" src="/images/picture.png" /></picture>
Active Storage blobs (images that are uploaded by the users of your app):
picture_tag(user.profile_picture)
# => <picture><img src="/rails/active_storage/blobs/.../profile_picture.webp" /></picture>
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'actionview/lib/action_view/helpers/asset_tag_helper.rb', line 491 def picture_tag(*sources, &block) sources.flatten! = sources..symbolize_keys = .delete(:image) || {} skip_pipeline = .delete(:skip_pipeline) content_tag("picture", ) do if block.present? capture(&block).html_safe elsif sources.size <= 1 image_tag(sources.last, ) else = sources.map do |source| tag("source", srcset: resolve_asset_source("image", source, skip_pipeline), type: Template::Types[File.extname(source)[1..]]&.to_s) end safe_join( << image_tag(sources.last, )) end end end |