2011-08-22 8 views
9

Estoy usando un clip para cargar archivos (videos e imágenes). Han utilizado el mismo archivo adjunto (fuente) para video e imágenes.Utilice un solo archivo adjunto para video/imagen en clip

class Media < ActiveRecord::Base 
    belongs_to :memory 
    validates_attachment_presence :source 
    validates_attachment_content_type :source, 
    :content_type => ['video/mp4', 'image/png', 'image/jpeg', 'image/jpg', 'image/gif'] 
end 

Ahora quería mostrar diferentes mensajes de error en casos diferentes.

  1. Al cargar el archivo está el tipo de imagen pero no el jpg/png/jpeg/gif.
  2. archivo
  3. Cuando subido es el tipo de vídeo pero no el mp4

¿Cómo puedo lograr esto? Cualquier ayuda sería muy apreciada.

Respuesta

22

Así que finalmente obtuve la solución. He añadido 2 validación condicional para el mismo

class Media < ActiveRecord::Base 
    belongs_to :memory 
    validates_attachment_presence :source 
    validates_attachment_content_type :source, 
    :content_type => ['video/mp4'], 
    :message => "Sorry, right now we only support MP4 video", 
    :if => :is_type_of_video? 
    validates_attachment_content_type :source, 
    :content_type => ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'], 
    :message => "Different error message", 
    :if => :is_type_of_image? 
    has_attached_file :source 

    protected 
    def is_type_of_video? 
    source.content_type =~ %r(video) 
    end 

    def is_type_of_image? 
    source.content_type =~ %r(image) 
    end 
end 
+0

nice solution. por cierto. ¿conserva un archivo adjunto por registro, el nombre de la clase no debería llamarse medio en este caso? – res

Cuestiones relacionadas