本文介绍了截断 rails 后不能使用 raw的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是如果我使用 '[---MORE---]', :length => 0))%> 然后我尝试打印它 - <%= raw @description %> 我仍然看到所有的 html 标签.

The trouble is if I use <% @description = (truncate(post.content, :separator => '[---MORE---]', :length => 0))%> and then I try to print it - <%= raw @description %> I still see all html tags.

推荐答案

truncate 默认对字符串进行转义,但您可以使用 :escape 选项将其关闭:

truncate escapes the string by default, but you can turn it off using :escape option:

@description = (truncate(post.content, :separator => '[---MORE---]', :length => 0, :escape => false))

其他方法是将 post.content 标记为 html 安全:

Other approach would be to mark the post.content as html safe:

truncate(post.content.html_safe, ...

如果你这样做,你甚至可以删除raw.

If you do this you can even remove the raw.

这篇关于截断 rails 后不能使用 raw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 00:04