Truncate text and not show unwanted characters with rails

3

I am using the truncate function to show part of a text, next to this function I added the function html_safe so that the text does not show unwanted characters. But when the text is greater than the limit I set, length: 150 , the unwanted characters reappear.

I would like a solution to this, truncate the text but do not show the unwanted characters.

<%= truncate(service.description.html_safe, length: 15, )
    
asked by anonymous 02.06.2016 / 21:17

2 answers

0

Gem truncate_html might solve your problem

Example input: <p>This is <em>my <strong>first</strong> post</em></p>

Using gem: <%= truncate_html post.title, :length => 15 %>

Output: <p>This is <em>my <strong>fir&hellip;</strong></em></p>

    
02.06.2016 / 21:54
0

<%= truncate(sanitize service.description, length: 15, ) %>

Use sanitize(html, options = {})

    
17.10.2018 / 15:44