Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails- display certian number of characters from a textfield

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 ??

like image 852
ChrisWesAllen Avatar asked Apr 15 '10 19:04

ChrisWesAllen


2 Answers

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)
like image 111
pkaeding Avatar answered Nov 14 '22 14:11

pkaeding


'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

like image 25
Himanshu Patel Avatar answered Nov 14 '22 15:11

Himanshu Patel