在 ruby​​ 中省略截断链接

编程入门 行业动态 更新时间:2024-10-23 13:24:15
本文介绍了在 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>

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

Rather than making the actual ... a link it shows the code. See: 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​​ 中省略截断链接

本文发布于:2023-11-03 14:59:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   ruby

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!