I'm using CarrierWave uploader for user avatars in my rails project and I'm currently providing a default URL (randomly chosen from 4 choices) for users who didn't upload their pictures. As suggested by the CarrierWave README, I've implemented it as thus:
class UserAvatarUploader < ImageUploader
def default_url(*args)
ActionController::Base.helpers.asset_path("avatars/default_avatar_#{rand(4).to_s}.png")
end
end
Since it is a random function, the problem here is that the user's avatar changes every time the page is reloaded. Ideally I would like to derive a number from the username of the user so that his/her default avatar stays consistent. How might I do that? Thanks in advance!
rand
you can use lets sayuser.id
or something if you want it to be unique? – udaydefault_url
method but I don't know how to – Jin Zhe