Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert plain text URLs to HTML hyperlinks in Ruby on Rails?

I have a text area into which users enter a lot of text that potentially includes hyperlinks. How can I display the text and have the URLs automatically appear as hyperlinks? Is there a gem, plugin, or existing method that does this? I'm looking to be able to do something like:
<%=h @my_object.description.with_links %> in my view.

like image 601
Clay Avatar asked Jun 22 '10 16:06

Clay


2 Answers

See rinku gem, which is apparently faster and more accurate than autolink gem

like image 182
Yarin Avatar answered Oct 10 '22 19:10

Yarin


Rails 3

Use the provided helper method called auto_link, which is part of ActionPack.

<%=h auto_link(@my_object.description) %>

Rails 4

Auto-linking has been removed from Rails and has been made into the rails_autolink gem.

require 'rails_autolink'

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>"
like image 39
JP Silvashy Avatar answered Oct 10 '22 20:10

JP Silvashy