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 <[email protected]> wrote:
> [SOLVED] should use :
> ActionController::Base.helpers.strip_tags(self[:body])
>
>
> On Jun 7, 4:37 pm, Erwin <[email protected]> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.