Truncate text and do not show unwanted characters with Rails

Asked

Viewed 249 times

3

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

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

<%= truncate(service.description.html_safe, length: 15, )
  • length: 150 or 15? You have one in the text and one in the code

  • The behavior is as expected. See: https://repl.it/repls/NovelPopularPercent What you need?

2 answers

0

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

Breathes with the method sanitize(html, options = {})

0

A Gem truncate_html maybe it’ll solve your problem

Input example: <p>This is <em>my <strong>first</strong> post</em></p>

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

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

  • It truncates the text as expected, what happens is that when the text is greater than the limit imposed by length it returns unwanted characters, if it is less than that it returns the text normally!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.