Ruby-on-rails 带有内部文本或 html 在 rails 中的 link_to image_tag
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5387122/
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
link_to image_tag with inner text or html in rails
提问by useranon
I want to output the following with Ruby on Rails link_toand image_tagmethods:
我想用 Ruby on Railslink_to和image_tag方法输出以下内容:
<a href="#">Lorem Ipsum <img src="/images/menu-arrow-down.gif"></a>
What would be a good way in Rails?
在 Rails 中什么是好方法?
采纳答案by corroded
Why not just use a link to and image tag?
为什么不只使用指向和图像标签的链接?
<%= link_to "hiii #{image_tag(yourimagepath)}", "link_path" %>
You could also try the appending done above (string + image_tag), but it will throw an error if one of those things becomes nil. Using interpolation, it will just show a blank image(or string) if it is nil.
您也可以尝试上面完成的附加 ( string + image_tag),但如果其中之一变为nil. 使用插值,它只会显示一个空白图像(或字符串)(如果是)nil。
For Rails 3, you will need to use html_safe:
对于 Rails 3,您需要使用html_safe:
<%= link_to "hiii #{image_tag(yourimagepath)}".html_safe, "link_path" %>
回答by Kevin Sylvestre
You can use blocks as an alternative to the string interpolation with correct usage html_safe. For example:
您可以使用块作为正确用法的字符串插值的替代方法html_safe。例如:
<%= link_to '#' do %>
Lorem Ipsum <%= image_tag('/images/menu-arrow-down.gif') %>
<% end %>
回答by Fazley
Or a shorter way is
或者更短的方法是
<%= link_to image_tag('image/someimage.png') + "Some text", some_path %>
回答by trying_hal9000
link_to(image_tag("image.png", :alt => "alt text", :class =>"anyclass"), image_url)
回答by James
I cannot seem to figure out how to add a comment to @corroded's answer. However, if you are using Rails 3, his answer will not work as expected. It might not be immediately obvious how to fix this, at least it wasn't for me. I knew I needed to add html_safe, but put it in the wrong place. In retrospect, it was obvious, but I thought I would include it here for others like me who make the "rookie" mistake.
我似乎无法弄清楚如何在@corroded 的答案中添加评论。但是,如果您使用的是 Rails 3,他的回答将不会按预期工作。如何解决这个问题可能不是很明显,至少它不适合我。我知道我需要添加html_safe,但把它放在错误的地方。回想起来,这是显而易见的,但我想我会把它包括在这里,让像我这样犯“菜鸟”错误的人。
<%= link_to "hiii #{image_tag(yourimagepath)}".html_safe, "link_path" %>
be careful though, if you are using a user assignable variable for "hiii" you need to sanitize it first.
不过要小心,如果您使用用户可分配的变量作为“hiii”,您需要先对其进行清理。
<%= link_to (h(my_model.some_string) + image_tag(yourimagepath)).html_safe,
"link_path" %>
Note that there is no issue with my_model.some_string or yourimagepath being nil because both h() and image_tag() return strings when passed nil.
请注意, my_model.some_string 或 yourimagepath 为 nil 没有问题,因为 h() 和 image_tag() 在传递nil时都返回字符串。
回答by Eric Woodruff
I prefer this approach in HAML
我更喜欢 HAML 中的这种方法
= link_to root_path do
= image_tag "ic_launcher.png", size: "16x16"
Home
回答by Eribos
Here's a cool way to use a link_to to include an image, and a mailto href with Text (displayed next to the image):
这是使用 link_to 包含图像和带有文本的 mailto href(显示在图像旁边)的一种很酷的方法:
<%= link_to image_tag("image.png") + " [email protected]",
href: "mailto:[email protected]" %>
Giving you something like this in your view (the entire image and text become an mailto href):
在你的视图中给你这样的东西(整个图像和文本变成一个mailto href):


回答by Ivasan
For me this worked just fine:
对我来说,这工作得很好:
<%= link_to(image_tag(<URL>,style),<URL>) %>
回答by Vishnu
i found out this also which worked.
我发现这也有效。
link_to(image_tag("/images/linkd.png",:alt=>"twt"){},:href=>"some_url",:target=>"_blank")

