Cornelius Wilson
Cornelius Wilson

Reputation: 2924

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

Upvotes: 0

Views: 154

Answers (2)

Nitin Jain
Nitin Jain

Reputation: 3083

You should also take care of scenario when you destroy the default image . you should set default_photo_id to nil or to any other photo if you want.

Upvotes: 0

Jeremy Green
Jeremy Green

Reputation: 8594

You could set up the user model to be linked directly to a default photo.

class User < ActiveRecord::Base
  belongs_to :default_photo, :class_name => "Photo"
end

You'd also need to add a default_photo_id column to the users table.

Then provide an interface that allows the user to browse all of their photos. In the UI you could have a button that says "Make default" (or whatever), and when the user clicks on that button it triggers a controller action that looks something like this:

def choose_default_photo
  @photo = Photo.find params[:photo_id]
  current_user.default_photo = @photo
  redirect_to '/profile' # or wherever you wan to send them
end

Then whenever you need to reference the model for the default photo you'd just use:

current_user.defaut_photo

Upvotes: 4

Related Questions