2016-06-22 30 views
0

相對較新的軌道上的紅寶石,我正在創建一個簡單的上傳模型的紙夾圖像被附加和顯示。我已經完成了所有工作,但我想限制當前用戶在任何給定時間上傳不超過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
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 
+0

的快速回復丹尼斯謝謝,但似乎並不爲工作我。我完全按照上述內容添加到我的上傳模型中,仍然允許我爲一個用戶附加六個以上的圖像。我的Paperclip附件的名稱是'圖片'而不是'圖像',這是否有關係? – Senator

+0

圖片是上傳模型的屬性。你的模型用戶has_many:上傳。你確定上傳是填充user_id屬性?,請在控制檯中嘗試。 User.first.uploads.count – DennisCastro

+0

這很奇怪,當我查看上傳模型中每個記錄的用戶屬性時,這是一個很長的不可識別的ID#,它對於每個記錄都是不同的。但是,user_id是該用戶的普通數字#設計在這裏做一些奇怪的事情嗎? – Senator