— Dec 03, 2012
TL;DR: Ever wanted to cut a long string down to something like “This is the song that n…”? Use Rails’
truncate
helper!
Warning: This is my first blog! Exciting, yes! But you’ll have to bear with my rambling as I get used to the process. :)
The other day I ran into the mundane issue of having a record’s title
attribute exceed the width that a
Twitter Bootstrap tooltip could display. I quickly threw together
some helper methods that would trim excessive title
s down to size:
Along with it’s specs, I was quite pleased with my solution. So much so I thought to myself “Ah hah! I should write a small gem to easily add this functionality to other projects!” Then it hit me. Such a common problem is likely already solved…
And there it was ActiveView::Helpers::TextHelper#truncate
.
The exact functionality I was (re)implementing. Don’t miss out on this little helper! Its simple: