Add ProfileImage from users previously uploaded photos
I have tried to do this but was unsuccessful. There's also no postings
covering this which is odd considering selecting Default photo is a option
for every social network. I have the CarrierWave gem and I want to set it
up so that the user can select their ProfileImage (default image) from
photos they have already uploaded. This photo would be used site wide.
It's like having an avatar, however the articles out there only show how
to upload a avatar as oppose to selecting a avatar from your uploaded
photos. I am sure this will be helpful to other people since this is a
common feature.
Photos controller:
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
User model:
# It is setup so no gallery is created, and photos are associated with the
user.
private
def setup_gallery
Gallery.create(user: self)
end
Photo model:
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
No comments:
Post a Comment