Paperclip - Validate File Type but not Presence

后端 未结 4 508
粉色の甜心
粉色の甜心 2020-12-30 14:21

I am using paperclip to handle my file uploads, and in one situation I don\'t want the file to be mandatory. I do however want to make sure it is a specific file type when i

4条回答
  •  情书的邮戳
    2020-12-30 14:52

    I guess you could try a 'conditional validation' where the condition is if a file is present?

    class TestModel < ActiveRecord::Base
      #stuff
      has_attached_file :sound #etc...
      validates_attachment_content_type :sound, :content_type => ['audio/mp3', 'application/x-mp3'], :if => :sound_attached?
    
      def sound_attached?
        self.sound.file?
      end
    end
    

提交回复
热议问题