Ruby output text on rails

How to get only the first (say) 200 characters from my Article object (which is in the table articleswith attributes contentand title)?

thanks

+3
source share
3 answers

TextHelper rails have a method truncate.

So in your views you just need to do:

<%= truncate @article.content, :length => 50 %>

Where 50 is the number of characters you want to display.

+9
source
irb(main):004:0> '0123456789'[3,7]
=> "3456789"
irb(main):005:0> '0123456789'[3..7]
=> "34567"
irb(main):006:0> '0123456789'[3...7]
=> "3456"

The above code and outputs are explanatory.

+1
source
0

Source: https://habr.com/ru/post/1722435/


All Articles