Ruby-on-rails 如何将图像转换为 Rails 中的链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5386318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I turn an image into a link in Rails?
提问by Spencer Cooley
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 标签做到这一点?
回答by Michelle Tilley
Use an image_tagfor the contents of the link_to.
使用image_tag了的内容link_to。
link_to image_tag("path/to/image.png"), "http://www.mysite.com/"
回答by Hyman Daniel
my solution:
我的解决方案:
<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some css class here" %>
<% end %>
回答by Pravin
Dry one
In your application_helper.rb
干一
在你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) %>
回答by Mike Lewis
<%= link_to(image_tag("path/to/image.png"), root_path) %>
Where root_pathis a route for your homepage of your site.
root_path您网站主页的路径在哪里。

