I have a database column calledcommentthat stores comments containing HTML tags.
To shorten large texts and display them completely in a popup window, I used thetruncate(comment, length: 50, escape: false)function.
Let us consider two examples:
Example 1: Thecommentcolumn contains the following plain text with HTML tags. By usingescape: false, the HTML tags are not truncated and the text is displayed correctly, including any formatting such as bold:
123
\\
test
Example 2: In this case, I'm using thehreftag to create the link, but theescapebehavior is not working as expected. It doesn't recognize it as an HTML tag, but treats it as plain text:
ClickToOpenFile
After truncation, the display is as follows:
I have tried using
rawandhtml_safe, but unfortunately they do not provide the desired results.
Use github.com/hgmnz/truncate_html gem:
- This is a link
' truncate_html(some_html, length: 15, omission: '...(continued)') =>