views:

66

answers:

2

Hi there, the Ruby code is below:

<%= product.advert_text -%>

What script should I add then can limit the words amount?

Many thanks for anyone's advice.

+5  A: 

If you want to limit to a certain number of words, the easiest way is going to be to split on space and then join the desired number of words back into a string.

<%=product.advert_text.split.slice(0, limit).join(" ") -%>
Chris Heald
perfect, thanks heaps
xuanyinwen
+8  A: 

Since you are working with Rails, you can use the truncate method.

So, something like:

<%= truncate(product.advert_text, :length => 20) -%>

The :length option sets the # of characters to truncate at & will add ellipses at the end (you can override the default ellipses with the :omission option - see the documentation on my link).

Brian
many thanks, it work great.
xuanyinwen