0

我正在使用 CarrierWave,到目前为止,该画廊对公众开放,没有所有权。我想设置一个,用户不必创建画廊。唯一的选择应该是将照片上传到他们的帐户,我想将每张用户照片上传限制为最多 5 张。因此,如果用户 16 登录,他们可以选择将最多 5 张照片上传到他们的个人资料。一旦达到该限制,如果用户尝试上传更多,它应该说“最大上传照片,删除以上传更多”。我不确定如何解决这个问题。

photo.rb 模型:

class Photo < ActiveRecord::Base
  attr_accessible :title, :body, :gallery_id, :name, :image, :remote_image_url
  has_many :user, :through => :gallery
  has_many :gallery
  mount_uploader :image, ImageUploader

    LIMIT = 5

    validate do |record|
      record.validate_photo_quota
    end

    def validate_photo_quota
      return unless self.user
      if self.gallery.user(:reload).count >= LIMIT
        errors.add(:base, :exceeded_quota)
      end
    end
end

照片控制器:

类照片控制器 < 应用控制器

  def new
    @photo = Photo.new(:gallery_id => params[:gallery_id])
  end

  def create
    @photo = Photo.new(params[:photo])
    if @photo.save
      flash[:notice] = "Successfully created photos."
      redirect_to @photo.gallery
    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

画廊控制器:

class GalleriesController < ApplicationController
  def index
    @galleries = Gallery.all
  end

  def show
    @gallery = Gallery.find(params[:id])
  end

  def new
    @gallery = Gallery.new
  end

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

  def edit
    @gallery = Gallery.find(params[:id])
  end

  def update
    @gallery = Gallery.find(params[:id])
    if @gallery.update_attributes(params[:gallery])
      flash[:notice] = "Successfully updated gallery."
      redirect_to gallery_url
    else
      render :action => 'edit'
    end
  end

  def destroy
    @gallery = Gallery.find(params[:id])
    @gallery.destroy
    flash[:notice] = "Successfully destroy gallery."
    redirect_to galleries_url
  end
end
4

1 回答 1

1

限制用户访问

为了限制用户访问某些模型,我会使用CanCan 之类的东西。

它会让你做这样的事情:

## ability.rb
# Allow user to CRUD pictures belonging to own gallery
can :manage, Picture, gallery: {user: user}

在控制器中,您可以执行以下操作:

# picture_controller.rb
# assuming a nested route, e.g. galleries/1/pictures
load_and_authorize_resource :gallery
load_and_authorize_resource :picture, through: :gallery

这将确保每个用户只能看到他或她自己的图片。

限制图库中的图片数量

我认为您的验证方法是可以的。

我会这样简化它:

validate :quota

private

def quota
  return unless user
  if gallery.pictures.count > 4
    errors[:base] << "Maximum photos uploaded, delete to upload more"
  end 
end

错误消息可能应该进入一个语言环境文件。

为每个用户自动创建图库

为此,请确保 Gallery 模型belong_to与 User 有关联。然后在 User 模型的回调中创建图库:

# models/user.rb
after_create :setup_gallery

private
def setup_gallery
  Gallery.create(user: self)
end

一般注意事项

定义has_many关系时,应使用复数名称,例如has_many :usershas_many :galleries

于 2013-04-03T19:28:21.857 回答