views:

39

answers:

1

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

+1  A: 

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)
pkaeding
In ruby you can also just give a range to a string. mystring[0..119]
Beanish