如何将图像转换成Rails中的链接?

时间:2021-02-28 20:45:37

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?

如何使用link_to rails标记实现这一点?

4 个解决方案

#1


60  

Use an image_tag for the contents of the link_to.

为link_to的内容使用一个image_tag。

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

#2


19  

my solution:

我的解决方案:

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

#3


10  

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) %>

#4


3  

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

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

root_path是站点主页的一个路径。

#1


60  

Use an image_tag for the contents of the link_to.

为link_to的内容使用一个image_tag。

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

#2


19  

my solution:

我的解决方案:

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

#3


10  

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) %>

#4


3  

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

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

root_path是站点主页的一个路径。