将用户名路由到根 URL 之外并防止冲突
我想像 twitter.com 那样(以及许多其他网站),并为我的用户提供如下 URL:
http://mysite.com/theirusername
目前,我的一个用户模型验证只是拒绝将用户名从硬编码列表中列入黑名单。
有没有比对列表进行硬编码更好的方法?例如,检查路线(代码示例?)
运营此类网站的人的任何智慧之言都将受到重视!我确信您一定遇到过这样的情况:您想要使用某个新功能的 URL,但有人正在使用它作为用户名。这种情况经常出现吗?有什么方法可以处理它?我猜你总是可以选择另一个名称,使用子域,或者要求用户重命名。也许将一些常见的英语名词列入黑名单是值得的?还有其他人吗?
I want to do like twitter.com does (and so many other sites do) and give my users URLs like:
http://mysite.com/theirusername
Presently, one of my User model validations simply denies blacklisted usernames off of a hardcoded list.
Is there a better way to do this than hardcoding the list? e.g. inspect the routes (code example?)
Any words of wisdom from someone who runs such a site would be valued! I'm sure you must have run into the scenario where you want to use a URL for some new feature, but someone is using it as their username. Has that come up often and what are some ways to handle it? You can always pick another name, use a subdomain, or ask the user to rename I guess. Perhaps it would be worthwhile to blacklist some common english nouns? Any others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此:
然后您可以执行诸如 http://mysite.com/theirusername 之类的操作。在
UsersController#show
操作中,您需要使用User.find_by_username(params[:id])
。如果某人已经在使用您想要将来使用的用户名,请向他们发送一封电子邮件,告知他们进行更改(您的条款和条件中也应该有类似的内容),并给他们一段时间来进行更改的改变。
您的目标应该是拥有一些黑名单功能,这很容易通过
User
模型中的单词列表来实现,然后使用validates_exclusion_of
进行检查。Use this:
Then you can do things like http://mysite.com/theirusername. Inside the
UsersController#show
action you will need to useUser.find_by_username(params[:id])
.If someone is already using a username that you want to use in the future, send them an email advising them of the change (you should have something to this effect in your Terms and Conditions as well) and give them a period of time to make the change.
You should aim to have some blacklisting feature, which is easy enough to do with a list of words in your
User
model which is then checked withvalidates_exclusion_of
.