【问题标题】:Can't use raw after truncate rails截断rails后不能使用raw
【发布时间】:2014-01-05 18:45:21
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby truncate


    【解决方案1】:

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

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

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

    truncate(post.content.html_safe, ...
    

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

    【讨论】:

    • 正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2021-10-09
    • 2019-08-23
    • 2014-02-07
    • 2021-08-25
    相关资源
    最近更新 更多