link_to image_tag与内部文本或HTML在轨道
我想用Ruby on Rails link_to和image_tag方法输出以下内容: 
 <a href="#">Lorem Ipsum <img src="http://img.dovov.commenu-arrow-down.gif"></a> 
Rails中有什么好方法?
为什么不使用链接和图像标签?
 <%= link_to "hiii #{image_tag(yourimagepath)}", "link_path" %> 
 你也可以尝试上面所做的追加( string + image_tag ),但是如果其中一个变为nil ,它将会抛出一个错误。 使用插值,如果它是nil ,它将只显示一个空白图像(或string)。 
 对于Rails 3,您将需要使用html_safe : 
 <%= link_to "hiii #{image_tag(yourimagepath)}".html_safe, "link_path" %> 
 您可以使用块作为正确使用html_safe的string插值的替代方法。 例如: 
 <%= link_to '#' do %> Lorem Ipsum <%= image_tag('http://img.dovov.commenu-arrow-down.gif') %> <% end %> 
或者更短的方法是
 <%= link_to image_tag('image/someimage.png') + "Some text", some_path %> 
 link_to(image_tag("image.png", :alt => "alt text", :class =>"anyclass"), image_url) 
我似乎无法弄清楚如何添加评论@腐蚀的答案。 但是,如果您使用的是Rails 3,他的回答将无法按预期工作。 如何解决这个问题可能并不明显,至less它不适合我。 我知道我需要添加html_safe ,但把它放在错误的地方。 回想起来,这很明显,但是我想我会把它包括在内,就像我这样的“菜鸟”的错误。
 <%= link_to "hiii #{image_tag(yourimagepath)}".html_safe, "link_path" %> 
但要小心,如果你正在用“hiii”用户可分配的variables,你需要先清理它。
 <%= link_to (h(my_model.some_string) + image_tag(yourimagepath)).html_safe, "link_path" %> 
请注意,my_model.some_string或yourimagepath不存在问题,因为h()和image_tag()在传递nil时会返回string。
我更喜欢HAML中的这种方法
 = link_to root_path do = image_tag "ic_launcher.png", size: "16x16" Home 
这里有一个很酷的方式来使用link_to来包含一个图像和一个mailto href与文本(显示在图像旁边):
 <%= link_to image_tag("image.png") + " some@email.com", href: "mailto:some@email.com" %> 
在你看来,给你这样的东西(整个图像和文字变成一个mailto href):

我发现这也是有效的。
 link_to(image_tag("http://img.dovov.comlinkd.png",:alt=>"twt"){},:href=>"some_url",:target=>"_blank")