Rails:为用户分配一个随机的默认头像(ala Twitter)?
我使用 Paperclip 来处理用户的头像,目前使用以下方法设置默认图像:
:default_url => '/images/img-missing_:style.png'
但我想做的是,当用户注册时,给他们一个随机头像,直到他们上传自己的头像。
Twitter 用它的“蛋”头像来做到这一点,并且可能有六种不同的颜色被分配。
我怎样才能做到这一点?
请注意,我不想每次调用用户时都显示随机头像,而是希望他们在注册时分配一个随机头像,并一直使用该头像,直到他们上传自己的头像。
I'm using Paperclip to handle avatar's for users and currently set a default image using:
:default_url => '/images/img-missing_:style.png'
But what I'd like to do is when a user registers, give them a random avatar that they get until they upload their own.
Twitter does this with its "egg" avatar and that have probably half a dozen different colors that get assigned.
How can I accomplish this?
Note, I'm not wanting to show a random avatar every time a call to the user is made, rather I want them to be assigned a random avatar when they register and have that one stick with them until they upload their own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有 10 个默认头像,您可以使用其用户 ID 的模数来确定显示哪一个。这将是一致的并且实施起来很简单。
Lets say you have 10 default avatars, you could use the modulus of their user ID to determine which one is displayed. This would be consistent and trivial to implement.
您还可以使用用户名长度来确定使用什么图片作为默认头像,假设我使用用户名注册:villain(7个字符),
您可以使用7作为引用图像编号7的方法(例如, 7.png) 设置为默认头像。
为了使其看起来更加一致,请添加以下子句:
因此,在上面的例子中,您可以使用 7 作为幻数,并且可以将头像 7.png 作为该用户的默认值。
You can also use the User Name length as a way to determine what picture is used as default avatar, let's say i sign up with username: villain (which is 7 characters)
you can use 7 as a means to reference image number 7 (say, 7.png) to set as the default avatar.
To make it look more consistent, add in clauses like:
So, in my case above, you can use 7 as the magic number and may be avatar 7.png as default for this user..