I have a database column calledcomment
that 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: Thecomment
column 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 thehref
tag to create the link, but theescape
behavior 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
raw
andhtml_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)') =>