user94154
user94154

Reputation: 16584

Ruby url to html link conversion

I'm building a simple chat app with Rails. when a user types in a url, I want it to be output as an html link (ie, "url").

I was wondering if there is any library or well known way to do this in Ruby. If not, I've got some decent regex sample code to work with...

Upvotes: 5

Views: 1489

Answers (1)

ryanb
ryanb

Reputation: 16287

Check out the auto_link helper method provided by Rails. This turns all URLs and email addresses into clickable links (html anchor tags). Here's a code sample from the docs.

auto_link("Go to http://www.rubyonrails.org and say hello to [email protected]")
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
#     say hello to <a href=\"mailto:[email protected]\">[email protected]</a>"

Upvotes: 10

Related Questions