0
如果提交的image_url以.gif,.jpg或.png結尾,則應驗證以下代碼。但是,我不確定我瞭解這段代碼"%r{\.(gif|jpg|png)\Z}i"
如何驗證格式。如何在Rails中驗證URL必須是.gif,.jpg或.png?
# Sample Code:
class Product < ActiveRecord::Base
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: { with: %r{\.(gif|jpg|png)\Z}i, message: 'must be a URL for GIF, JPG or PNG image.'
}
end
只是爲了告訴你我沒有測試你的正則表達式這驗證文件。希望它是正確的。鏈接有正則表達式。你可以善用它 – Athar