method
image_tag
![Very extensive documentation Importance_5](https://d2vfyqvduarcvs.cloudfront.net/images/importance_5.png?1349367920)
image_tag(source, options = {})
public
Returns an html image tag for the source. The source can be a full path or a file that exists in your public images directory.
Options
You can add HTML attributes using the options. The options supports three additional keys for convenience and conformance:
- :alt - If no alt text is given, the file name part of the source is used (capitalized and without the extension)
- :size - Supplied as "{Width}x{Height}", so "30x45" becomes width="30" and height="45". :size will be ignored if the value is not in the correct format.
- :mouseover - Set an alternate image to be used when the onmouseover event is fired, and sets the original image to be replaced onmouseout. This can be used to implement an easy image toggle that fires on onmouseover.
Examples
image_tag("icon") # => <img src="/images/icon" alt="Icon" /> image_tag("icon.png") # => <img src="/images/icon.png" alt="Icon" /> image_tag("icon.png", :size => "16x10", :alt => "Edit Entry") # => <img src="/images/icon.png" width="16" height="10" alt="Edit Entry" /> image_tag("/icons/icon.gif", :size => "16x16") # => <img src="/icons/icon.gif" width="16" height="16" alt="Icon" /> image_tag("/icons/icon.gif", :height => '32', :width => '32') # => <img alt="Icon" height="32" src="/icons/icon.gif" width="32" /> image_tag("/icons/icon.gif", :class => "menu_icon") # => <img alt="Icon" class="menu_icon" src="/icons/icon.gif" /> image_tag("mouse.png", :mouseover => "/images/mouse_over.png") # => <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" /> image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # => <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
Register or
log in
to add new notes.
hardbap -
September 30, 2011 - (>= v3.0.0)
grosser -
July 10, 2008
RobinWu -
October 31, 2008
Ramon -
March 10, 2010
yeuem1vannam -
June 26, 2013 - (v3.2.13)
![Default_avatar_30](https://www.gravatar.com/avatar/c147704f8d15cdfb50973f10f8c11965?default=http://apidock.com/images/default_avatar_30.png&size=30)
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 %>
![Default_avatar_30](https://www.gravatar.com/avatar/59436ecd4fe6ad7c34f67654d839f05f?default=http://apidock.com/images/default_avatar_30.png&size=30)
0 thanks
watch out for urls with &
image_tag(‘x.com/aaa?a=1&b=2’) = x.com/aaa?a=1&b=2
![Default_avatar_30](https://www.gravatar.com/avatar/7a23526e8de7f2fa57d2481b549b6d57?default=http://apidock.com/images/default_avatar_30.png&size=30)
0 thanks
By images's sub dirctionary to img tag
image_tag(“icons/edit.png”) # =>
<img src="/images/icons/edit.png" alt="edit" />
![Default_avatar_30](https://www.gravatar.com/avatar/fbd9cb107fe7c941333d6a3488691989?default=http://apidock.com/images/default_avatar_30.png&size=30)
0 thanks
Specify your own template
You can specify you own template this way:
def notice ... @template = "some_other_name.html.erb" end
![Default_avatar_30](https://www.gravatar.com/avatar/278cfe6e981866752e97f52765862674?default=http://apidock.com/images/default_avatar_30.png&size=30)
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+$}