Reputation: 1579
I am using CKEditor for my blogging app, and am saving formatting data in my :content
attribute.
For example, a particular @post.content may start out like this.
<p>\r\n\t<span class=\"s1\" style=\"color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.1em; line-height: 1.7em;\">This is my text starting here. This is an really awesome entry, you see...
The first part of the entry is all formatting data, and the actual written content starts with "This is my text starting here..."
What I would like to do is display the first fifty characters of the written content.
I tried something like this,
<%= post.content.first(50).try(:html_safe) %>
But this doesn't return anything, unless the formatting data is less than fifty characters.
How can I go about displaying the first fifty characters of written content?
Please let me know if this is unclear, or if any additional info is needed. Thanks much for your help!
Upvotes: 0
Views: 213
Reputation: 8807
One way to do that would be to use Nokogiri to extract out the raw text.
require 'nokogiri'
text = Nokogiri::HTML(post.content).text.strip
The above would give you raw text on which you can use the truncate
method to display the first 50 characters.
Upvotes: 0
Reputation: 8169
try using strip_tags method
e.g.
<%= truncate(strip_tags(post.content), :length => 50, :omission => '..').html_safe %>
Upvotes: 5