Skip to content

Instantly share code, notes, and snippets.

@xgotyou
xgotyou / images_helper.rb
Last active October 5, 2018 17:49
Rails images for retina and other high dpi displays using srcset
module ImagesHelper
# Outputs html img tag with srcset attribute for 2x image based on original
# src. Use naming convention ex. having 'image.jpg', 2x image should be named
# '[email protected]'. If there's no image.jpg or [email protected] exception will be
# thrown by Rails asset pipeline.
def retina_image(src, options = {})
src2x = src.gsub(/(^.+)(\.(jpg|png)$)/, '\1@2x\2')
image_tag src, options.merge(srcset: "#{image_url(src2x)} 2x")
end
end