Rails-display certian number of characters from text field

I am running a rails project and I am showing a text box that can often be too long. If there are any things that I can call to display only 20 words or 120 characters on the watch page?

+7
ruby ruby-on-rails text
source share
2 answers

You may be interested in the TextHelper truncate function:

truncate("Once upon a time in a world far far away") # => Once upon a time in a world f... truncate("Once upon a time in a world far far away", :length => 14) # => Once upon a... truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)") # => And they found that many (clipped) truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 15) # => And they found... (continued) 
+14
source share
 'Once upon a time in a world far far away'.truncate(27) "Once upon a time in a wo..." 'Once upon a time in a world far far away'.truncate(27, separator: ' ') "Once upon a time in a..." 'Once upon a time in a world far far away'.truncate(27, separator: /\s/) "Once upon a time in a..." 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)') "And they f... (continued)" 

You can also see this example.

truncate

+4
source share

All Articles