image_tag(source, options = {}) public

Returns an image tag converting the options into html options on the tag, but with these special cases:

  • :alt - If no alt text is given, the file name part of the src is used (capitalized and without the extension)
  • :size - Supplied as "XxY", so "30x45" becomes width="30" and height="45"

The src can be supplied as a…

  • full path, like "/my_images/image.gif"
  • file name, like "rss.gif", that gets expanded to "/images/rss.gif"
  • file name without extension, like "logo", that gets expanded to "/images/logo.png"
Show source
Register or log in to add new notes.
September 30, 2011 - (>= v3.0.0)
1 thank

Using a block with image_tag

HTML5 officially supports block-level elements in the anchor tag and Rails 3 allows you to pass a block to image_tag:

<%= image_tag(some_path) do %>

<%= content_tag(:p, "Your link text here")

<% end %>

July 10, 2008
0 thanks

watch out for urls with &

image_tag(‘x.com/aaa?a=1&b=2’) = x.com/aaa?a=1&amp;b=2

October 31, 2008
0 thanks

By images's sub dirctionary to img tag

image_tag(“icons/edit.png”) # =>

<img src="/images/icons/edit.png" alt="edit" />
March 10, 2010
0 thanks

Specify your own template

You can specify you own template this way:

def notice
  ...
  @template = "some_other_name.html.erb"
end
June 26, 2013 - (v3.2.13)
0 thanks

Typing mismatch

This block

if size = options.delete(:size)
  options[:width], options[:height] = size.split("x") if size =~ %{^\d+x\d+$}
end

has type mismatch

%r{^\d+x\d+$}