我已经看过并用Google搜索,发现了许多漂亮的网址链接。
但它们看起来都像http://example.com/users/username
我希望它像github style http://example.com/username
我可以遵循的任何指示?
答案 0 :(得分:2)
http://example.com/users/username出了什么问题?使用http://example.com/username的问题是,有人可以非常轻松地注册可能与您的其他路由冲突的用户名admin
或faq
。您需要保留一个禁止的用户名列表,并且在创建新路由之前还需要检查用户是否存在具有给定名称的用户,因为这可能与现有用户冲突。
您可以随时将其缩短为:http://example.com/u/username。
但是,如果你必须这样做,你可以像其他人所建议的那样使用map.connect
,但你一定要考虑它的优点和缺点,以及它是否会增加任何重要价值。
答案 1 :(得分:1)
一个非常基本的例子可能是:
在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)
生成链接。
答案 2 :(得分:0)
这不就是你只需要在路线中删除users/
吗?
map.connect '/:username',...
代替map.connect '/users/:username',...