Friday, January 4, 2013

truncate and link text helper in rails


snippet to truncate text and link to see more

Code this in application helper.

  def truncate_and_link( text,options ={})
    length = options[:length]
    return text if length.blank?
    url = options[:url] || '#'
    output = raw truncate(text, :length => length)
    output += link_to('more', url) if text.size > length
    output.html_safe
  end

Useful when eg linking big posts

No comments:

Post a Comment