如何设置 github.com 像用户名 url 一样?

发布于 2024-08-19 13:18:39 字数 299 浏览 9 评论 0原文

我已经看过并用谷歌搜索并发现了许多漂亮的网址链接。

但它们看起来都像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春夜浅 2024-08-26 13:18:39

http://example.com/users/username 有什么问题吗?使用 http://example.com/username 的问题是有人可以很容易地注册用户名 adminfaq 这可能与您的其他路由冲突。您需要保留禁止的用户名列表,并且在创建新路由之前需要检查是否存在具有给定名称的用户,因为这可能会与现有用户冲突。

您始终可以将其缩短为: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 or faq 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.

云淡风轻 2024-08-26 13:18:39

一个非常基本的示例可能是这样的:

  • 声明一个具有最低优先级的路由,指向用户控制器的显示操作
  • 修改显示操作以根据用户名查找记录

config/routes.rb中:

map.username '/:username', :controller => :users, :action => :show

UsersController中

def show
  @name = Name.find_by_name(params[:username]) || Name.find(params[:id])
end

然后您可以使用 username_path(:username => user.name) 生成链接。

A very Basic example could be this:

  • declare a route with lowest priority pointing to your users controller's show action
  • modify the show action to find records based on username

In config/routes.rb:

map.username '/:username', :controller => :users, :action => :show

In UsersController:

def show
  @name = Name.find_by_name(params[:username]) || Name.find(params[:id])
end

You can then use username_path(:username => user.name) to generate links.

千紇 2024-08-26 13:18:39

是不是您只需要在路由中添加 users/ 即可?

map.connect '/:username',... 而不是 map.connect '/users/:username',...

Isn't that you just need to drop users/ in routes?

map.connect '/:username',... instead of map.connect '/users/:username',...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文