从上传的文件中选择头像

时间:2013-10-01 18:35:56

标签: ruby-on-rails carrierwave

我安装了carrierwave,用户可以上传照片。如何设置,以便用户不必上传头像文件,而是可以从他们上传的照片中选择哪一个将被表示为他们的头像?例如,当您在Facebook上时,点击其中一张照片,点击“选项”链接,它会显示“制作个人资料照片”。然后,该图像将成为您在Facebook中使用的头像或图片。我正在寻找同样的事情。

图库控制器:

 def index
    @galleries = Gallery.all
  end

  def show
    @gallery = Gallery.find(id_params)
  end

  def new
    @gallery = Gallery.new
  end

  def create
    @gallery = Gallery.new(gallery_params)
    if @gallery.save
      flash[:notice] = "Created gallery."
      redirect_to @gallery
    else
      render :action => 'new'
    end
  end

  def edit
    @gallery = Gallery.find(id_params)
  end

  def update
    @gallery = Gallery.find(id_params)
    if @gallery.update_attributes(gallery_params)
      flash[:notice] = "Updated gallery."
      redirect_to gallery_url
    else
      render :action => 'edit'
    end
  end

  def destroy
    @gallery = Gallery.find(id_params)
    @gallery.destroy
    flash[:notice] = "Gallery deleted."
    redirect_to galleries_url
  end

  private


   def gallery_params
     params.require(:user).permit(:name)
   end

   def id_params
     params.require(:id).permit(:name)
   end

end

照片控制器:

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

1 个答案:

答案 0 :(得分:1)

以下是我可能会做的一个例子

class UserAvatarController < ApplicationController
  def edit
    @gallery = current_user.gallery
    # render gallery of photos for user to choose
  end

  def update
    if params[:photo_id].present?
      current_user.update_attributes avatar_id: params[:photo_id]
    else
      flash[:error] = "No photo selected"
      render action: "edit"
    end
  end
end