How to Automatically Link URLs in Rails

Let’s say you have a Rails app and you’d like any URLs in any plain text string to be automatically hyperlinked. Well, then you’ll need this:
.gsub(/((http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?/.*)?)/, '<a href='1'>1</a>')

For example:
I blog at http://garrickvanburen.com

Through:
"I blog at http://garrickvanburen.com".description.gsub(/((http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?/.*)?)/, '<a href='1'>1</a>')

Results in:
I blog at http://garrickvanburen.com

One thought on “How to Automatically Link URLs in Rails

Comments are closed.