2013-09-29 90 views
0

這裏是我的代碼:驗證錯誤。這裏有什麼問題?

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: 'must be a URL for GIF, JPG, or PNG image.' 
} 
end 

這裏是我收到的錯誤:

所提供的正則表達式使用多錨(^或者$),這可能會帶來安全風險。你的意思是使用\ A和\ z,還是忘了添加:multiline => true選項?

第5行

顯然存在錯誤時,我感到非常新的Rails。實際上,這是我的第一天。有什麼問題,我該如何解決它?謝謝你的時間。

+1

答案出現在您的錯誤消息中:在'with::'中使用'\ z'而不是'$'。 –

+0

非常感謝。 –

回答

1

我有同樣的問題。通過書中的例子。 這是我工作的代碼:

class Product < ActiveRecord::Base 
    validates :title, :description, :image_url, presence: true 
    validates :title, uniqueness: true 
    validates :price, numericality: {greater_than_or_equal_to: 0.01} 
    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