Is there an HTML safe truncate method in Rails?

后端 未结 10 667
执笔经年
执笔经年 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 06:06

    We can do that with the help of simple_format http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

    html = "1234567890"
    simle_format(truncate_markup(html, :length => 5)) 
    

    => "123 456 7890"

提交回复
热议问题