代码之家  ›  专栏  ›  技术社区  ›  Shpigford

Rails:为用户分配一个随机的默认头像(ala twitter)?

  •  1
  • Shpigford  · 技术社区  · 15 年前

    我在用 Paperclip 要为用户处理头像,并当前使用以下方法设置默认图像:

    :default_url => '/images/img-missing_:style.png'

    但我想做的是当一个用户注册时,给他们一个随机的虚拟人物,直到他们上传自己的。

    Twitter用它的“蛋”化身来实现这一点,并且可能有六种不同的颜色被分配。

    我怎样才能做到这一点?

    注意,我不想每次给用户打电话时都显示随机的虚拟人物,而是希望他们在注册时被分配一个随机的虚拟人物,并且让他们一直使用这个虚拟人物,直到他们上传自己的虚拟人物。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Spencer Hakim    15 年前

        2
  •  0
  •   Jasdeep Singh    15 年前

     0) First, check that the username falls within a plausible character length.. (say 6-14)
     1) if number is even, divide by 2 and use the result to pick up the image.
     2) if result is odd, use the number as it is to pick up the default avatar.