How do I turn an image into a link in Rails?

I just want to do a simple link on an image.

<a href="http://www.mysite.com"><img src="path/to/image.png"/></a>

How do you do this with a link_to rails tag?


Solution 1:

Use an image_tag for the contents of the link_to.

link_to image_tag("path/to/image.png"), "http://www.mysite.com/"

Solution 2:

my solution:

<%= link_to root_path do %>
   <%= image_tag "image.jpg", class: "some css class here" %>
<% end %>

Solution 3:

Dry one
In your application_helper.rb

def link_to_image(image_path, target_link,options={})
  link_to(image_tag(image_path, :border => "0"), target_link, options)
end

And then from your views

<%= link_to_image("path/to/image", some_url) %>

Solution 4:

<%= link_to(image_tag("path/to/image.png"), root_path) %>

Where root_path is a route for your homepage of your site.