带有内部文本或 html 在 rails 中的 link_to image_tag [英] link_to image_tag with inner text or html in rails

查看:15
本文介绍了带有内部文本或 html 在 rails 中的 link_to image_tag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Ruby on Rails link_toimage_tag 方法输出以下内容:

I want to output the following with Ruby on Rails link_to and image_tag methods:

<a href="#">Lorem Ipsum <img src="/images/menu-arrow-down.gif"></a>

在 Rails 中什么是好方法?

What would be a good way in Rails?

推荐答案

为什么不直接使用链接和图片标签?

Why not just use a link to and image tag?

<%= link_to "hiii #{image_tag(yourimagepath)}", "link_path" %>

你也可以尝试上面的附加(string + image_tag),但是如果其中之一变成nil,它会抛出一个错误.使用插值,如果它是 nil,它只会显示一个空白图像(或字符串).

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.

对于 Rails 3,您需要使用 html_safe:

For Rails 3, you will need to use html_safe:

<%= link_to "hiii #{image_tag(yourimagepath)}".html_safe, "link_path" %>

这篇关于带有内部文本或 html 在 rails 中的 link_to image_tag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆