开发者

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

开发者 https://www.devze.com 2023-01-31 17:56 出处:网络
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 t开发者_运维知识库he commen

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 t开发者_运维知识库he 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消