新手问题 在这种出现 routes 冲突时大家会怎样做呢?

heylonj · December 15, 2014 · Last by libuchao replied at December 15, 2014 · 1966 hits

相信这样的情况大家都遇到过。 routes.rb

get "/:user_name", to: "users#show" 
resources :categories
....

如果有人注册了个 user_name 为"categories"时就会出现 routes 冲突。想问一下大家都是如何解决的呢?

  1. 限制一部分用户名的注册,或在 seed 里面把所有冲突用户名先导入,让“/:user_name”优先级最低。
  2. 改用“/users/:user_name”,恩,url 长了点,不太优雅呀。
  3. 还有其他的好方法么?

routes

get 'user/:username'     => 'users#show'
get ':username'          => 'users#show'

helper

RESERVE_USERNAMES = %w(login register logout categories)

def user_path user
  if RESERVE_USERNAMES.include? user.username.downcase
    '/user/' + user.username
  else
    '/' + user.username
  end
end

两行换位

我惯用的是 ~:username

我也好奇无前缀的网站要怎么解决用户名冲突:

Github 只有不活动用户会被回收 https://help.github.com/articles/name-squatting-policy/

Twitter: We reserve the right at all times (but will not have an obligation) to remove or refuse to distribute any Content on the Services, to suspend or terminate users, and to reclaim usernames without liability to you. https://twitter.com/tos

Facebook: 如果您为您的帐户或主页选择了一个用户名或类似的标识符,我们将保留在适当的时候将其删除或回收的权利(例如某商标所有人投诉用户名与用户的实际名字相去甚远)。 https://www.facebook.com/legal/terms

#1 楼 @libuchao great,谢谢分享,这个方法确实可以避开这个问题,只要维护一个小 list。

#2 楼 @huobazi 这样就放弃了一小戳用户名哇

#3 楼 @Rei 感谢分享 ref。似乎目前没有一个完美的解决方法啊。好像我多年前注册个 explorer 的 id,结果网站添加个浏览(/explorer)功能,我就抓瞎啦,哈哈。

#5 楼 @heylonj 教育你的用户“舍小家保大家” 👌

这个问题我一直在考虑。需要搞个黑名单啊。比如谁注册个 id 叫 admin 也很难看。

/@username 比较时髦

You need to Sign in before reply, if you don't have an account, please Sign up first.