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

Comments

Popular posts from this blog

Modulo operation in Ruby

Difference between rake db:migrate db:reset and db:schema:load

Add Pidgin to Ubuntu 12:10 notification menu