【发布时间】:2013-07-19 13:04:16
【问题描述】:
有如下代码:
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)$}i,
message: 'URL must point to GIT/JPG/PNG pictures'
}
end
它可以工作,但是当我尝试使用“rake test”对其进行测试时,我会收到以下消息:
rake aborted!
The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option?
这是什么意思?我该如何解决?
【问题讨论】:
-
你试过
/\.(gif|jpg|png)$/i吗?也许%r{}在末尾添加了它自己的$。 -
@Wukerplank 我不这么认为。
%r{\.(gif|jpg|png)$}i #=> /\.(gif|jpg|png)$/i,%r{\.(gif|jpg|png)}i #=> /\.(gif|jpg|png)/i. -
是的,但没有帮助
-
@wukerplank 我们为什么要猜测?在 ruby 中,我们有
irb来帮助我们确定:) -
你需要把png后面的$去掉,表示这是最后一项,用\z替换
标签: ruby-on-rails ruby activerecord