andrewpthorp
andrewpthorp

Reputation: 5096

Make sure html elements are closed when showing a substring on a webpage (ruby on rails)

Let's say I am doing this:

entries.each do |entry| entry[0,1000] + "..." end

Let's say that entry has a <ul> and a <li> that were not closed, because it chopped the entry in the middle of a list. How can I make sure to close those tags out, so rendering isn't messed up?

I was considering creating a method that found the last index of <ul> and made sure it was less than the last index of </ul>, etc. This seems cumbersome though. Any ideas on how to solve this with ruby on rails?

Thanks!

Upvotes: 1

Views: 124

Answers (1)

Heikki
Heikki

Reputation: 15417

Check out the hpricot gem:

Hpricot("<ul><li>foo<ul><li>bar").to_html
=> "<ul><li>foo<ul><li>bar</li></ul></li></ul>"

Upvotes: 2

Related Questions