在 ruby​​ 中省略截断链接 [英] Make omission in ruby truncate a link

查看:35
本文介绍了在 ruby​​ 中省略截断链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让省略号..."成为我截断的 ruby​​ 字符串的链接.这是我所拥有的:

I'd like to make the omission "..." a link for my truncated ruby string. Here's what I have:

  <%= truncate(testimony.testimony, :length => 125, :omission => (link_to "...", testimony)) %><br />

但它是这样做的:

Etiam porta sem malesuada magna mollis euismod. Aenean lacinia bibendum nulla sed consectetur<a href="/testimonies/1">...</a>

而不是制作实际的...链接,它显示了代码.请参阅:http://cl.ly/4Wy3 获取屏幕截图.

Rather than making the actual ... a link it shows the code. See: http://cl.ly/4Wy3 for the screenshot.

谢谢!

推荐答案

问题是 truncate santizes 输出,您需要使用 raw() ,如下面的文档所示:

The problem is that truncate santizes the output , you need to use raw() as in the docs below:

结果未标记为 HTML 安全,因此在视图中使用时将受到默认转义的影响,除非由 raw() 包装.如果文本包含 HTML 标签或实体,则应小心,因为截断可能会产生无效的 HTML(例如不平衡或不完整的标签).

The result is not marked as HTML-safe, so will be subject to the default escaping when used in views, unless wrapped by raw(). Care should be taken if text contains HTML tags or entities, because truncation may produce invalid HTML (such as unbalanced or incomplete tags).

编辑示例:

<%= raw(truncate(testimony.testimony, :length => 125, :omission => (link_to "...", testimony))) %><br />

这篇关于在 ruby​​ 中省略截断链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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