instance method
truncate
Ruby on Rails 3.0.20
Since v2.2.3Signature
truncate(text, options = {})
Truncates a given text after a given :length if text is longer than :length (defaults to 30). The last characters will be replaced with the :omission (defaults to “…”) for a total length not exceeding :length.
Pass a :separator to truncate text at a natural break.
The result is not marked as HTML-safe, so will be subject to the default escaping when used in views, unless wrapped by raw(). Care should be taken if text contains HTML tags or entities, because truncation may produce invalid HTML (such as unbalanced or incomplete tags).
Examples
truncate("Once upon a time in a world far far away") # => "Once upon a time in a world..." truncate("Once upon a time in a world far far away", :length => 17) # => "Once upon a ti..." truncate("Once upon a time in a world far far away", :length => 17, :separator => ' ') # => "Once upon a..." truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)') # => "And they f... (continued)" truncate("<p>Once upon a time in a world far far away</p>") # => "<p>Once upon a time in a wo..."
Parameters
-
textreq -
optionsopt = {}
Source
# File actionpack/lib/action_view/helpers/text_helper.rb, line 85
def truncate(text, options = {})
options.reverse_merge!(:length => 30)
text.truncate(options.delete(:length), options) if text
end
Defined in actionpack/lib/action_view/helpers/text_helper.rb line 85
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::TextHelper