user2759575
user2759575

Reputation: 553

Truncating html_safe text leaving unclosed tags

I'm truncating some text like this:

<%= sanitize(post.body_text, tags: %w(p img)).truncate(20, :separator => " ").html_safe %>

The problem is if an img or p tag is caught in between 20 characters it cuts off like so:

<p>This is some text <img...

Which messes up the html of the rest of the page. So how do I truncate without leaving unclosed tags?

Upvotes: 1

Views: 714

Answers (1)

radubogdan
radubogdan

Reputation: 2834

I had this problem before. Check out this gem

Upvotes: 1

Related Questions