Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert links from a string to clickable links using Ruby/Rails

Using Ruby/Rails does anyone know how to take a large string that may contain some HTML elements and make them into links?

This is an example:

"Check out my video on you tube http://youtu.be/OkCcD6cOKgs"

I am looking for something that will turn the HTML into a valid click-able link <a href ... but also leave the other text as is, just like this question did.

like image 978
John D Avatar asked Apr 15 '13 00:04

John D


People also ask

How to convert url to clickable hyperlink using linkify?

In order to convert url into a clickable hyperlink, we just need to wrap the Text widget containing url object inside Linkify widget, as shown below: Linkify widget has text property in which we passed the url object we parsed from our News class. The url is now displayed as an actual clickable link, but this is only half job done.

How do you use link_to in rails?

The first argument for link_to is the text on the link. The second argument? It’s the URL you’re linking to. You can hardcode it if you want, but most of the time you’ll be using a Rails model, or a _path method. Rails will figure things out when you follow the proper conventions.

How to use Erb link in rails?

When you click the ERB link it will go to main page in browser. The two types of linking tags are doing the same thing in Rails. Above the page linking tags are used in the URL in Rails.These URL links are most used in action and controllers, Now we will see one example. Above the URL address there are two additional parameters.

What are the two types of linking tags in rails?

The two types of linking tags are doing the same thing in Rails. Above the page linking tags are used in the URL in Rails.These URL links are most used in action and controllers, Now we will see one example. Above the URL address there are two additional parameters.


2 Answers

I know it is too late, But if someone still have this question then he can do this way.

text = "Check out my video on you tube http://youtu.be/OkCcD6cOKgs"
html_text = text.gsub(URI.regexp, '<a href="\0">\0</a>').html_safe

This is best way I found from here Ruby: make plain text links clickable till now.

like image 170
MUHAMMAD SOBAN Avatar answered Sep 18 '22 22:09

MUHAMMAD SOBAN


Rails used to have a built in "auto_link" function, which now can be brought in as an 'autolink' gem. This does other string sanitization too.

Alternatively the 'anchored' gem is more focused on this one task.

Without gems, here's the ruby code I have used:

  url_regexp = %r{
    (?:(?:https?|ftp|file):\/\/|www\.|ftp\.)
    (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|
         [-A-Z0-9+&@#\/%=~_|$?!:,.])*
    (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|
          [A-Z0-9+&@#\/%=~_|$])
  }ix

  text = text.gsub(url_regexp, '<a href="\0">\0</a>').html_safe

I recommend you carefully test some edge cases with whatever solution you go for. Looking around for answers to this you'll find people recommending the core library URI.regexp or URI::DEFAULT_PARSER, but this fails very simple tests. Try including a word followed by a colon ':' char in your text. You don't want that to become a link! (I don't really know why this built-in solution is so poor. My guess that is it's more intended for validating rather than finding URL-like substrings)

So my solution above is a regexp based on this answer and converted to be more rubyish. But when you start looking, there are many different regexps for URLs out there. You can play the game of finding the awkward edge case that any given regexp fails on. Generally longer regexps catch more edge cases. But there's also a challenge to convert it to work in ruby. This website presents many language solutions including a (quite long) ruby example: http://urlregex.com

like image 29
Harry Wood Avatar answered Sep 21 '22 22:09

Harry Wood