想利用 gravater 为用户提供一个默认的头像。希望通过 carrierwave 从 Gravatar 获取头像,并做一个缩小后的版本保存在本地。我试图在 user before_create 中做处理,但是失败了,avatar 中依然为空。以下是代码...
class User
mount_uploader :avatar, AvatarUploader
before_create do |user|
gravatar_url = Gravatar.new(user.email).image_url
user.avatar = open(gravatar_url)
end
end
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"upload/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def scale(width, height)
process resize_to_fit: [64, 64]
end
version :tiny do
process resize_to_fit: [24, nil]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end