Is there an HTML safe truncate method in Rails?

后端 未结 10 662
执笔经年
执笔经年 2020-12-24 05:45

I have a string of HTML in Rails. I\'d like to truncate the string after a certain number of characters not including the HTML markup. Also, if the split happens to fall in

10条回答
  •  無奈伤痛
    2020-12-24 05:55

    the regular truncate function works fine, just pass :escape => false as an option to keep the HTML intact. eg:

    truncate(@html_text, :length => 230, :omission => "" , :escape => false)
    

    RubyOnRails.org

    *Edit I didn't read the question very carefully (or at all TBH), so this answer does not solve this question... It IS the answer I happened to be looking for though, so hopefully it helps 1 or 2 people :)

提交回复
热议问题