Limiting characters/words in view - ruby on rails

前端 未结 4 2008
情深已故
情深已故 2021-02-03 20:43

I am displaying recent comments on the home page of a very simple blog application I am building in Ruby on Rails. I want to limit the number of characters that are displayed fr

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 21:34

    If you are using rails 4.2 or above then you can use truncate_words method.

    For example:
    "In a world where everything is awesome".truncate_words(3)

    Output: "In a world..."

提交回复
热议问题