writing an excerpt from a post body

the post body contains html tags , so I cannot directly truncate it... I thought I could use sanitize to clean up the htl tags then truncate but it doesn't work ...

  def write_excerpt     sanitized_body = ActionController::Base.helpers.sanitize(self[:body])     self[:excerpt] = ActionController::Base.helpers.truncate(sanitized_body, :length => 300) unless sanitized_body.length < 300   end

as an example I get : "<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar odio lorem non turpis. Nullam sit amet enim. Donec sed tellus eget sapien fringilla nonummy. Mauris a ante. Suspendisse quam sem, consequat at, commodo vit..."

there is a <p> tag ... which means that they are not stripped out ...

am I wrong ?

[SOLVED] should use : ActionController::Base.helpers.strip_tags(self[:body])

At the beginning of this year I wrote gem ‘html_slicer’ to simplify HTML truncation stuff like that.

The general idea was to implement pagination ability for long htmlised articles and strings, and on the other hand to resize included

Basic setup:

  class Article < ActiveRecord::Base
slice :content, :as => :paged, :slice => {:maximum => 300, :text_break => "..."}, :resize => {:width => 300}
end

Try it.

https://rubygems.org/gems/html_slicer