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?
Use an image_tag
for the contents of the link_to
.
link_to image_tag("path/to/image.png"), "http://www.mysite.com/"
my solution:
<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some css class here" %>
<% end %>
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) %>
<%= link_to(image_tag("path/to/image.png"), root_path) %>
Where root_path
is a route for your homepage of your site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With