3

I am trying to do this truncate raw(@some_text), length: 300 . When the text exceeds the limit of 300 characters I see html tags in the text.

I need to truncate and implement html(tags prepended and appended) properties in the text. Is there any other way to do the same? Thanks in Advance.

railer
  • 97
  • 7

4 Answers4

2

This should work raw(@some_text.slice(0,300))

wayne
  • 393
  • 2
  • 13
1

Your problem is by truncating you'll be removing the closing tags. You're basically going to need to strip all the tags if you need to truncate it.

http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

j-dexx
  • 10,286
  • 3
  • 23
  • 36
0

Try this to both truncate and remove html tags from the text....

truncate(strip_tags(@some_text), :length => 300)

Reference Link

Vamsi Krishna
  • 3,742
  • 4
  • 20
  • 45
  • this makes text to lose it's html properties. i.e tags that preceed or succeed text should be implemented and truncated. – railer Mar 04 '14 at 09:49
0

Here's code we use for this:

#app/helpers/application_helper.rb
def clean(content, length)
    body = sanitize(content, tags: [])
    truncate(body, length: length, separator: " ")
end

If you put that into a helper, you can call: clean(@some_text, "300")

Richard Peck
  • 76,116
  • 9
  • 93
  • 147