Pregunta

En nuestra última aplicación que necesitamos para procesar algunos archivos, he trabajado con un clip antes y todo funciona! pero estamos dando carrierwave una oportunidad, pero parece prometedor, no puedo encontrar cómo validar el tamaño de un archivo adjunto , parece que la documentación no tiene ninguna información al respecto, debemos añadir manualmente a el modelo a través de un validador personalizado?

Gracias de antemano!

¿Fue útil?

Otros consejos

I've made an Active Model File Validators gem that checks content type and file size validation for Carrierwave, PaperClip, Drangonfly, Refile (hopefully it will work with other uploading solutions). It detects the content type based on the content of the file and it has a media type spoof detector. It works both before and after uploads.

Since 1.0 version CarrierWave has built-in file size validation.

Install latest carrierwave gem

gem 'carrierwave', '~> 1.0'

Add method size_range to provide a min size and a max size

class ImageUploader < CarrierWave::Uploader::Base
  def size_range
    0..2.megabytes
  end

In model add validates_integrity_of to valid a file size (and content type) of an image.

class Image < ApplicationRecord
  mount_uploader :image, ImageUploader

  validates_integrity_of :image

Here is the solution that I came up with - the trick was that I couldn't check the file size directly as that made the Fog RubyGem bomb if the file hadn't been uploaded. I would expect there to be a cleaner way to ask CarrierWave if a file was uploaded.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top