我在我的网站上有用户生成的评论。如果用户在评论中添加URL,我希望将其格式化为链接,并实际链接到该URL。我怎么做?

最佳答案

Rails有一个auto_link文本帮助器。

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>"

auto_link("Visit http://www.loudthinking.com/ or e-mail [email protected]", :link => :urls)
# => "Visit <a href=\"http://www.loudthinking.com/\">http://www.loudthinking.com/</a>
#     or e-mail [email protected]"

10-08 19:14