Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 566
Spencer Cooley Avatar asked Mar 22 '11 03:03

Spencer Cooley


4 Answers

Use an image_tag for the contents of the link_to.

link_to image_tag("path/to/image.png"), "http://www.mysite.com/"
like image 117
Michelle Tilley Avatar answered Oct 22 '22 14:10

Michelle Tilley


my solution:

<%= link_to root_path do %>
   <%= image_tag "image.jpg", class: "some css class here" %>
<% end %>
like image 30
Jack Daniel Avatar answered Oct 22 '22 16:10

Jack Daniel


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) %>
like image 11
Pravin Avatar answered Oct 22 '22 14:10

Pravin


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

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

like image 7
Mike Lewis Avatar answered Oct 22 '22 14:10

Mike Lewis