Working with disinfectants and trimming can be challenging. There are many different sanitation assistants: h, CGI :: escapeHTML, sanitize, strip_tags, html_safe, etc. Sanitation and truncation do not work well if the string is truncated between the opening and the closing tag or in the middle right of the special HTML character.
The following statement seems to work
sanitize(text, :tags=>[]).truncate(30, :separator => " ").html_safe
The focus in the pass a: separator option is to cut off the text in a natural break.
source share