Displaying Only the first x words of a string in rails

前端 未结 4 836
眼角桃花
眼角桃花 2020-12-31 08:47
<%= message.content %>

I can display a message like this, but in some situations I would like to display only the first 5 words of the st

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 09:30

    In rails 4.2 you can use truncate_words.

    'Once upon a time in a world far far away'.truncate_words(4)
    => "Once upon a time..."
    

提交回复
热议问题