railer
railer

Reputation: 97

Are truncate and raw mutually exclusive methods in ruby?

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

Answers (4)

Richard Peck
Richard Peck

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

wayne
wayne

Reputation: 412

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

Upvotes: 2

Vamsi Krishna
Vamsi Krishna

Reputation: 3782

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

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

Reference Link

Upvotes: 0

j-dexx
j-dexx

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

Related Questions