0

对不起,我在这里束手无策。

我有这个

  validates :departing_at, :format => {
    # 2012-07-14 10:00 am
    :with => /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}) ([ap]m)$/,
    :message => "must be date and time"
  }

但它只是不会验证 2012-07-14 10:00 am 或与该正则表达式匹配的任何其他日期格式。

但是如果我把它放在控制器里面

pattern = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}) ([ap]m)$/

if params[:vacation][:departing_at] =~ pattern
  raise "works"
else
  raise params[:vacation][:departing_at].to_yaml
  raise "doesn't work"
end 

它打印出“作品”,伙计们有什么想法吗?:(

4

1 回答 1

0

试试这个矫枉过正的正则表达式:

:with => /^(\d{4})\-(\d{2})\-(\d{2})\s(\d{2})\:(\d{2})\s([aApP][mM])$/, 
于 2012-07-27T13:56:00.020 回答