Forum: Ruby on Rails Limiting the number of caracter displayed

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.
weirdmonkey (Guest)
on 2006-03-22 18:06
Hi!

I have a list of items with description but would like to show only the
first words of the description and link to the full item detail page.

How can I do that ?

Thank you!
ChrisT (Guest)
on 2006-03-22 19:02
(Received via mailing list)
weirdmonkey wrote:
> Hi!
>
> I have a list of items with description but would like to show only the
> first words of the description and link to the full item detail page.
>
> How can I do that ?
>
> Thank you!
>
>
Think truncate is what you need.

See
http://api.rubyonrails.org/classes/ActionView/Help...

Then you can just do a link_to with "more info" as the text.
weirdmonkey (Guest)
on 2006-03-22 19:15
Wow !

Thank you very much Chris!

I just need to add the my view <%= product.description.truncate %> right
?

(i'm a beginner when it comes to dynamic websites so excuses my poor
knowledge hehe)

++
ChrisT (Guest)
on 2006-03-22 19:37
(Received via mailing list)
No prob. I'm a newb myself, though have done a little php. :-)

Actually what you need is: <%= truncate(product.description, 50, ":::")
%>

Those last two parameters are optional and I've just chosen 50 and ":::"
arbitrarily. It defaults to 30 (characters) and "..."
This topic is locked and can not be replied to.