Reputation: 97
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.
Upvotes: 3
Views: 307
Reputation: 76774
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")
Upvotes: 0
Reputation: 3782
Try this to both truncate and remove html tags from the text....
truncate(strip_tags(@some_text), :length => 300)
Upvotes: 0
Reputation: 10406
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
Upvotes: 1