Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 rails 4中的图像验证_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 rails 4中的图像验证

Ruby on rails 4 rails 4中的图像验证,ruby-on-rails-4,Ruby On Rails 4,如何编写图像验证格式,确保url以.png、.jpg或.gif结尾 class Product < ActiveRecord::Base mount_uploader :image_url validates :title, :presence => true, :uniqueness => true validates :image_url, :presence => true, :

如何编写图像验证格式,确保url以.png、.jpg或.gif结尾

class Product < ActiveRecord::Base

  mount_uploader :image_url

  validates :title, :presence => true,
            :uniqueness => true
  validates :image_url, :presence => true,
                        :format => {
                           :with => %r{\.(gif|jpg|png)$}i,
                           :message => 'must be a URL for GIF, JPG or PNG image.'
                        }
end
类产品true,
:唯一性=>true
验证:image\u url,:presence=>true,
:格式=>{
:with=>%r{\.(gif | jpg | png)$}i,
:message=>'必须是GIF、JPG或PNG图像的URL。'
}
结束
我试过了,但没用

谢谢,
Mezbah

您的代码是正确的。您能告诉我什么不起作用吗?是否收到任何错误?提供的正则表达式正在使用多行定位(^或$),这可能会带来安全风险。您是想使用\A和\z,还是忘记添加:multiline=>true选项?