Rails file_field大小限制 [英] Rails file_field size restrictions

查看:103
本文介绍了Rails file_field大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前具有以下用于上传图像的内容,但我还想添加一个限制,以防止用户上传height中小于50px的任何文件.这可能吗?

I currently have the below for uploading images, but I also want to add a restriction that prevents users from uploading any file less than 50px in height. Is this possible?

file_field "form", "image", accept: 'image/png,image/gif,image/jpeg'

推荐答案

尝试一下,也许这会为您提供帮助:

Try this one, Maybe this will help you: Reference

validate :validate_image_size

def validate_image_size
  image = MiniMagick::Image.open(picture.path)
  unless image[:height] < 50
    errors.add :image, "should be 50px minimum!" 
  end
end

如果您使用MiniMagick,这将为您工作.

This will work for you if you use MiniMagick.

这篇关于Rails file_field大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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