Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Given a block of text, auto-link links

on mysite I have the ability to add comments. Sometimes users enter comments will links (href. ...)

I would like to have those links to be clickable/linkable (a href) when the comment is displayed to users.

how with Rails 3 can I take a comment, and look for links and then wrap those links in an a href tag that opens in a new window?

Thanks

like image 869
AnApprentice Avatar asked Dec 20 '10 21:12

AnApprentice


2 Answers

The simplest way is to use the auto_link method built into rails.

Note: In Rails 3.1 auto_link has been moved into a separate gem.

like image 178
idlefingers Avatar answered Nov 04 '22 09:11

idlefingers


idlefinger's suggestion of #auto_link is perfect. I know it's not the question you originally posed, but wanted to suggest: also check out #simple_format, which will nicely format your users' use of newlines into br and p tags.

like image 28
Jason Morrison Avatar answered Nov 04 '22 10:11

Jason Morrison