How to Automatically Link URLs in Rails
4 Jun 2007 in Programming, Ruby on Rails by GarrickLet’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
Comment | Trackback URL
Short Link:
Add a Comment
Related Entries
- Ruby on Rails Snippet for Changing Relative Paths to Absolute
- Using Names in Rails Routes Instead of IDs
- REST-less with link_to
- Introducing the gFeed
- Workaround for IE Overly Accepting in Rails’ respond_to format
