我正在使用回形针上传文件(视频和图像)。
视频和图像使用相同的附件(源)。
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
现在我想在不同的情况下显示不同的错误消息。
- 当上传文件是图像类型而不是jpg/png/jpeg/gif时。
- 当上传的文件是视频类型而不是mp4时
我怎样才能做到这一点?
任何帮助将不胜感激。
所以最后我得到了解决方案。
我为相同的内容添加了 2 个条件验证
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)