Forum: Ruby on Rails Truncate function and html tags

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
744b6438d1457d332d1a8d431cc2b641?d=identicon&s=25 Andrew Ohnstad (livitup)
on 2009-03-02 20:58
(Received via mailing list)
I have a text field in the db for the body of user posts.  Users
submit the posts using the tinymce editor, which allows certain html
tags.  The posts are stored in the db with allowed html tags directly
in the post, so I could have something like:

<p>This post will try to eliminate the confusion regarding rails and
the truncate function.</p>

I am trying to display the first 300 characters of the post on a
"what's new" type page.  I am using the truncate function of rails to
grab the first bit of each post for the what's new page...

truncate(post.text, 300)

which works just fine, except I have times where the 300 character
limit hits in the middle of a tag.  One such post is currently
screwing up my layout... the 300 character limit is hitting in the
middle of the "close paragraph" tag, so my output html is:

<p>This post will try to eliminate the confusion regarding rails and
the truncate function.</

Voiding the </div> that closes this area out.

Simple ideas to fix this welcome... :)
Feeab6a10f05f7962e314c5e43548157?d=identicon&s=25 rob-twf (Guest)
on 2009-03-02 22:54
(Received via mailing list)
On Mar 2, 7:57 pm, "andrew.ohns...@gmail.com"
<andrew.ohns...@gmail.com> wrote:
> grab the first bit of each post for the what's new page...
>
> Voiding the </div> that closes this area out.
>
> Simple ideas to fix this welcome... :)

Take a look at henrik's truncate_html helper:
http://henrik.nyh.se/2008/01/rails-truncate-html-helper
744b6438d1457d332d1a8d431cc2b641?d=identicon&s=25 Andrew Ohnstad (livitup)
on 2009-03-03 00:31
(Received via mailing list)
That's perfect... I knew someone had already invented the wheel! :)

Thanks!
359b7a7fc263382baee62d91c1e4c25e?d=identicon&s=25 Emanuele Tozzato (mekdigital)
on 2009-04-29 19:02
(Received via mailing list)
https://rails.lighthouseapp.com/projects/8994/tick...
This could help, too! :P

On Mon, Mar 2, 2009 at 12:57 PM, andrew.ohnstad@gmail.com <
andrew.ohnstad@gmail.com> wrote:

> "what's new" type page.  I am using the truncate function of rails to
> the truncate function.</
>
> Voiding the </div> that closes this area out.
>
> Simple ideas to fix this welcome... :)
> >
>


--
Emanuele Tozzato
+1 (619) 549 3230
1985 Sherington Place, #E302
Newport Beach, CA 92663
http://mekdigital.com
This topic is locked and can not be replied to.