如何设置 github.com 像用户名 url 一样?
我已经看过并用谷歌搜索并发现了许多漂亮的网址链接。
但它们看起来都像 http://example.com/users/username
我希望它就像 github 风格 http://example.com/username
我可以遵循的任何指示吗?
I've seen and googled and found many pretty url links.
But they all seem to look like http://example.com/users/username
I want it to be like github style http://example.com/username
Any directions that I can follow??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://example.com/users/username 有什么问题吗?使用 http://example.com/username 的问题是有人可以很容易地注册用户名
admin
或faq
这可能与您的其他路由冲突。您需要保留禁止的用户名列表,并且在创建新路由之前需要检查是否存在具有给定名称的用户,因为这可能会与现有用户冲突。您始终可以将其缩短为:http://example.com/u/username。
但是,如果您必须这样做,您可以按照其他人的建议使用
map.connect
,但您绝对应该考虑其优点和缺点以及它是否会增加任何重要的价值。What's wrong with http://example.com/users/username? The problem with using http://example.com/username is that someone could very easily register the username
admin
orfaq
which could conflict with your other routes. You'll need to keep a list of prohibited usernames and also you'll need to check if a user exists with a given name before you create a new route, as that might conflict with an existing user.You could always shorten it to: http://example.com/u/username.
However, if you must do this you can use
map.connect
as others have suggested, but you should definitely think about the pros and cons and whether or not it adds any significant value.一个非常基本的示例可能是这样的:
在
config/routes.rb
中:在
UsersController中
:然后您可以使用
username_path(:username => user.name)
生成链接。A very Basic example could be this:
In
config/routes.rb
:In
UsersController
:You can then use
username_path(:username => user.name)
to generate links.是不是您只需要在路由中添加
users/
即可?map.connect '/:username',...
而不是map.connect '/users/:username',...
Isn't that you just need to drop
users/
in routes?map.connect '/:username',...
instead ofmap.connect '/users/:username',...