Topic: how to auto link

What would be a good way to "auto link" words with @ in front of them like twitter does?

For example auto_link_user('Hello @matt please send file to') would return

'Hello <a href="/users/matt">@mat</a> please send file to'

Last edited by sthapit (2011-04-18 09:22:22)

Re: how to auto link

text = "hey I was talking to @barney and they said @george was more fun than @fred."

text.gsub(/@([^ ,.]+)/) { |match| link_to "#{match}", "/users/#{$1}" }
 => "hey I was talking to <a href=\"/users/barney\">@barney</a> and they said <a href=\"/users/george\">@george</a> was more fun than <a href=\"/users/fred\">@fred</a>."