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

We are happy to use Rinku

gem to auto-correct my text. This is useful when someone is pasting a URL in a comment - Rinku

will automatically set that URL.

However, really long URLs do interfere with the page layout. This would be helpful for every hyperlink encountered:

  • Shorten hyperlink text
  • Keep a basic hyperlink


may appear as http://www.ya...

, but in basic HTML a hyperlink

. Twitter does this with tweets.

Looked for high and low results for any existing gemstones or previous experience. I haven't thought of anything yet.


source to share

2 answers

It looks like Rinku supports customizing link text:

auto_link(text) do |url|
    url.truncate :length => 15




Don't know about Rinku, but you can easily do it from the view:

<% trunk_url = truncate(url, :length => 15) %><%= link_to(trunk_url, url) %>


Basically, truncate the url itself into a string (trunk_url) and then use that as the text for the link_to. In my case, the url was a field ( Works great.



All Articles