MKu*_*mar 9 validation file-upload paperclip ruby-on-rails-3
我正在使用回形针上传文件(视频和图像).对视频和图像使用了相同的附件(源).
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
Run Code Online (Sandbox Code Playgroud)
现在我想在不同情况下显示不同的错误消息.
我怎样才能实现这一目标?任何帮助将非常感谢.
MKu*_*mar 23
所以最后我得到了解决方案.我为此添加了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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4239 次 |
| 最近记录: |