如何在Facebook或Twitter等标签中标记用户呢? [英] How to tag users in feed like Facebook or Twitter does?
问题描述
我正在开发一个rails应用程序。我想在facebook或twitter上实现用户标记。当用户在Feed中发帖时,他可以在开始时用@字符标记任何用户。
HI I am working on a rails application. I want to implement user tagging as facebook or twitter does. When user is posting in feed he can tag any user with @ character at starting.
我正在使用 Jquery UI自动完成插件。
我有这个参考
实施jquery UI自动填充功能,以便在您键入@时显示建议我实现自动完成部分。
I have this reference Implementing jquery UI autocomplete to show suggestions when you type "@" which helped me to implement auto complete part.
但是现在我想将自动完成的用户名链接到用户个人资料网址,例如Username = Mak然后应该生成链接
But now I want to link that auto completed username to users profile url e.g Username = Mak then link should be generated like
<a href="http://www.example.com/username">Username</a>
所以请指导我如何在我的rails应用程序中实现这个?是否有任何宝石可以这样做。
So please guide me how can I implement this in my rails application?Is there any gem to do so.
推荐答案
嘿,它不需要上述方法。我是用简单的正则表达式做的。
Hey it doesn't required above methods. I did it using simple regex.
<% @str = @madyrocks solved problem of @username tagging %>
<%=@result_string = @str.gsub(/(\^|\s|\B)@(([a-zA-Z])(_?[a-zA-Z0-9]+)*|_([a-zA-Z0-9]+_?)*)/i,
%Q{ <a href="http://example.com/\\2">\@\\2</a>}) %>
在我的正则表达式中,我使用了(\ ^ | \ | \ B)
因为@可以在字符串的开头或标记用户时出现空格后出现。
In my regex I have used (\^|\s|\B)
because @ can occur at start of string or after a white space while tagging a user.
([a-zA-Z])(_?[a-zA-Z0-9] +)* | _([a -zA-Z0-9] + _?)*
这用于验证用户名。在我的应用程序中,用户名必须以字母开头,然后是字母和&数字。
([a-zA-Z])(_?[a-zA-Z0-9]+)*|_([a-zA-Z0-9]+_?)*
This is used to validate username. In my application username must start with alphabet followed by alphabets & numbers.
没有。 2 用于在正则表达式中进行第二场比赛。
The no. 2 used to take second match in regex.
有关详细信息,请参阅Rubular.com上的正则表达式
For more details try the regex on Rubular.com
我希望这会有所帮助。
这篇关于如何在Facebook或Twitter等标签中标记用户呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!