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

eg. http://www.yahoo.com

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

, but in basic HTML a hyperlink http://www.yahoo.com

. 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.

+3


source to share


2 answers


It looks like Rinku supports customizing link text:



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

      

+7


source


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 (tm.website). Works great.

0


source







All Articles