截断轨道后不能使用raw

Ale*_*kov 1 ruby truncate ruby-on-rails

麻烦的是,如果我使用<% @description = (truncate(post.content, :separator => '[---MORE---]', :length => 0))%>然后我尝试打印它 - <%= raw @description %>我仍然看到所有的HTML标签.

mec*_*ish 9

truncate默认情况下会转义字符串,但您可以使用以下:escape选项将其关闭:

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

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

truncate(post.content.html_safe, ...
Run Code Online (Sandbox Code Playgroud)

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