我曾尝试这样做,但没有成功。考虑到选择默认照片是每个社交网络的一个选项,也没有涉及此内容的帖子,这很奇怪。我有 CarrierWave gem,我想对其进行设置,以便用户可以从他们已经上传的照片中选择他们的 ProfileImage(默认图像)。这张照片将在网站范围内使用。这就像拥有一个头像,但是那里的文章只显示如何上传头像,而不是从您上传的照片中选择头像。我相信这对其他人会有所帮助,因为这是一个常见功能。
照片控制器:
def new
@photo = Photo.new
end
def create
@photo = Photo.new(params[:photo])
@photo.user = current_user
if @photo.save
flash[:notice] = "Successfully created photos."
redirect_to :back
else
render :action => 'new'
end
end
def edit
@photo = Photo.find(params[:id])
end
def update
@photo = Photo.find(params[:id])
if @photo.update_attributes(paramas[:photo])
flash[:notice] = "Successfully updated photo."
redirect_to @photo.gallery
else
render :action => 'edit'
end
end
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
flash[:notice] = "Successfully destroyed photo."
redirect_to @photo.gallery
end
end
用户型号:
# It is setup so no gallery is created, and photos are associated with the user.
private
def setup_gallery
Gallery.create(user: self)
end
照片模型:
attr_accessible :title, :body, :gallery_id, :name, :image, :remote_image_url
belongs_to :gallery
has_many :gallery_users, :through => :gallery, :source => :user
belongs_to :user
mount_uploader :image, ImageUploader
LIMIT = 5
validate do |record|
record.validate_photo_quota
end
def validate_photo_quota
return unless self.user
if self.user.photos(:reload).count >= LIMIT
errors.add(:base, :exceeded_quota)
end
end
end