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 ? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[SOLVED] should use : ActionController::Base.helpers.strip_tags(self[:body]) On Jun 7, 4:37 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote:> 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 ?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 <iframe>, <img>, <object> tags, having a width parameter (YouTube objects for example). 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 On 07.06.2012, at 18:46, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> [SOLVED] should use : > ActionController::Base.helpers.strip_tags(self[:body]) > > > On Jun 7, 4:37 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: >> 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 ? > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.