module ActionView::Helpers::TextHelper
def truncate(text, options = {})
Once upon a time in a wo..."
truncate("
Once upon a time in a world far far away
")# => "And they f... (continued)"
truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)')
# => "Once upon a..."
truncate("Once upon a time in a world far far away", :length => 17, :separator => ' ')
# => "Once upon a ti..."
truncate("Once upon a time in a world far far away", :length => 17)
# => "Once upon a time in a world..."
truncate("Once upon a time in a world far far away")
==== Examples
or entities, because truncation may produce invalid HTML (such as unbalanced or incomplete tags).
used in views, unless wrapped by raw(). Care should be taken if +text+ contains HTML tags
The result is not marked as HTML-safe, so will be subject to the default escaping when
Pass a :separator to truncate +text+ at a natural break.
for a total length not exceeding :length.
(defaults to 30). The last characters will be replaced with the :omission (defaults to "...")
Truncates a given +text+ after a given :length if +text+ is longer than :length
def truncate(text, options = {}) options.reverse_merge!(:length => 30) text.truncate(options.delete(:length), options) if text end