Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

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

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

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

Reference Link

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")

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top