I running a rails project and I am displaying a textfield that can often be too long. If there any things I can call to only display 20 words or 120 characters on the the view page ??
You may be interested in TextHelper's 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)
'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 see this example too
truncate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With