简体   繁体   中英

How can I limit the amount of words displayed from a Ruby string?

the Ruby code is below:

<%= product.advert_text -%>

What script can limit the amount of words?

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).

如果要限制为一定数量的单词,最简单的方法是分割空格,然后将所需数量的单词加回到字符串中。

<%=product.advert_text.split.slice(0, limit).join(" ") -%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM