在回形针中使用单个附件进行视频/图像 [英] Use single attachment for video/image in paperclip

查看:294
本文介绍了在回形针中使用单个附件进行视频/图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用回形针上传文件(视频和图像)。
对视频和图片使用了相同的附件(来源)。

  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

现在我想在不同情况下显示不同的错误信息。
$ b $ ol

  • 当上传文件是图片类型而不是jpg / png / jpeg / gif。
  • 当上传的文件是视频类型而不是mp4的时候

  • 我怎样才能做到这一点?
    任何帮助将不胜感激。

    解决方案

    最后我得到了解决方案。
    我为相同的

      class Media< ActiveRecord :: Base 
    belongs_to:memory
    validates_attachment_presence:source
    validates_attachment_content_type:source,
    :content_type => ['video / mp4'],
    :message => 抱歉,现在我们只支持MP4视频,
    :if => :is_type_of_video?
    validates_attachment_content_type:source,
    :content_type => ['image / png','image / jpeg','image / jpg','image / gif'],
    :message => 不同的错误信息,
    :if => :is_type_of_image?
    has_attached_file:源

    保护
    def is_type_of_video?
    source.content_type =〜%r(video)
    end

    def is_type_of_image?
    source.content_type =〜%r(image)
    end
    end


    I am using paperclip for uploading file ( video and images). Have used the same attachment(source) for both video and images.

    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
    

    Now I wanted to display different error messages in different cases.

    1. When uploading file is image type but not the jpg/png/jpeg/gif.
    2. When uploaded file is video type but not the mp4

    How can i achieve this ? Any help would highly appreciated.

    解决方案

    So finally I got the solution. I added 2 conditional validation for the same

    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
    

    这篇关于在回形针中使用单个附件进行视频/图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆