附加验证大小上传

时间:2017-06-27 14:41:41

标签: ruby-on-rails cloudinary

我想为附加上传添加验证(:photo_presentation)它应该超过0.5兆字节,少于2兆字节,请问有什么想法吗?

这是我的模特:

class User < ApplicationRecord


  has_attachment :photo_presentation
  has_attachment :photo_company_logo

  has_many :projects, dependent: :destroy
  has_many :articles

  include FriendlyId
  friendly_id :company, :use => :slugged

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  #geocoder for google maps
  geocoded_by :address
  after_validation :geocode, if: :address_changed?

  validates :email, uniqueness: true, presence: true
  validates :company, presence: true, uniqueness: true
  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :office_phone, uniqueness: true
  validates :mobile_phone, presence: true, uniqueness: true
  validates :address, presence: true
  validates :description, presence: true, length: { minimum: 300, maximum: 900 }
  validates :radius, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 50 }
  validates :city, presence: true
  validates :zip_code, presence: true
end

谢谢!

1 个答案:

答案 0 :(得分:0)

你可以尝试这个命令

validates_attachment :photo_presentation, :size => { :in => 0.5..2.megabytes }