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

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

Install Multimedia codec (offline) without internet connection in Ubuntu

rails 3.2 with jruby and mysql with rvm in ubuntu