如何限制用户上传的次数?

时间:2016-06-22 23:55:20

标签: ruby-on-rails ruby ruby-on-rails-4 paperclip

相对较新的ruby on rails我正在创建一个简单的Uploads模型,用于附加和显示Paperclip图像。我已经完成了所有工作,但我想限制当前用户在任何给定时间上传不超过6张图像。

我会添加什么文件和代码?对于我创建的任何未来模型,了解这将是非常有帮助的!

我假设这是一小段代码,但我无法在网上找到答案...谢谢!

我的UploadsController(做了一个简单的脚手架和回形针设置):

class UploadsController < ApplicationController
      before_action :set_upload, only: [:show, :edit, :update, :destroy]

      # GET /uploads
      # GET /uploads.json
      def index
        @uploads = Upload.all
      end

      # GET /uploads/1
      # GET /uploads/1.json
      def show
      end

      # GET /uploads/new
      def new
        @upload = current_user.uploads.build
      end

      # GET /uploads/1/edit
      def edit
      end

      # POST /uploads
      # POST /uploads.json
      def create
        @upload = current_user.uploads.build(upload_params)

        respond_to do |format|
          if @upload.save
            format.html { redirect_to @upload, notice: 'Upload was successfully created.' }
            format.json { render :show, status: :created, location: @upload }
          else
            format.html { render :new }
            format.json { render json: @upload.errors, status: :unprocessable_entity }
          end
        end
      end

      # PATCH/PUT /uploads/1
      # PATCH/PUT /uploads/1.json
      def update
        respond_to do |format|
          if @upload.update(upload_params)
            format.html { redirect_to @upload, notice: 'Upload was successfully updated.' }
            format.json { render :show, status: :ok, location: @upload }
          else
            format.html { render :edit }
            format.json { render json: @upload.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /uploads/1
      # DELETE /uploads/1.json
      def destroy
        @upload.destroy
        respond_to do |format|
          format.html { redirect_to uploads_url, notice: 'Upload was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_upload
          @upload = Upload.find(params[:id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def upload_params
          params.require(:upload).permit(:upload_title, :upload_description, :upload_datecreated, :user_id, :picture, :delete_picture)
        end
    end

上传模型:

 class Upload < ActiveRecord::Base
        belongs_to :user
        has_attached_file :picture, styles: { large: "600x600#", medium: "300x300#", small: "150x150#", thumb: "50x50#" }, default_url: "/images/:style/missing.png"
        validates_attachment_content_type :picture, content_type: /\Aimage\/.*\Z/

        before_validation { image.clear if @delete_image }

        def delete_picture
          @delete_image ||= false
        end

        def delete_picture=(value)
          @delete_image  = !value.to_i.zero?
        end
      end

1 个答案:

答案 0 :(得分:1)

class Upload < ActiveRecord::Base

  MAX_IMAGES = 6
  validate :maximum_images

  private

  def count_valid_images
    self.user.uploads.count
  end

  def maximum_images
    errors.add(:base, "must have max #{MAX_IMAGES} images") if count_valid_images > MAX_IMAGES
  end


end