0
想創建一個自定義的驗證方法,但得到一個錯誤正則表達式匹配與自定義驗證軌道
wrong number of arguments (0 for 1..3)
我想驗證郵政編碼的格式,但提供自定義錯誤消息,而不是消息我得到,如果我是這樣做
validates_format_of :postcode, with: /\A^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])[ ]{0,1}[0-9][ABD-HJLNP-UW-Z]{2})$\z/i, message: 'Please check your postcode'
這會給
postcode Please check your postcode
的錯誤信息所以我雖然我能做到這一點
class User < ActiveRecord::Base
validate :format_postcode
def format_postcode
regexp = /\A^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])[ ]{0,1}[0-9][ABD-HJLNP-UW-Z]{2})$\z/i
if !postcode.match(regexp)
errors.add[:base] << "Please check your Postcode"
end
end
end
但這引發錯誤
我到底做錯了什麼?
由於
感謝,愚蠢的錯誤真的...感謝指出,我結束了使用錯誤[:基地] <<「請檢查您的郵政編碼」 – Richlewis 2014-10-07 13:38:45