Daniel May Daniel May - 1 year ago 87
HTML Question

How to truncate hyperlink text but not hyperlink itself in Ruby on Rails?

Happily using

gem to autolink my text. This is helpful when someone pastes in a URL in a comment -
will autolink that URL.

However, really long URLs are messing with the page layout. It would be helpful for every hyperlink encountered:

  • Shorten the hyperlink text

  • Keep the underlying hyperlink

may be displayed as
but in the underlying HTML, the hyperlink is
. Twitter does this with tweets.

Have been searching high and low for any existing gems or prior experience on this. Haven't come up with anything so far.

Answer Source

Looks like Rinku supports customization of the link text:

auto_link(text) do |url|
    url.truncate :length => 15
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download