如何在rails 4.1中验证date_select

时间:2014-10-01 06:33:24

标签: ruby-on-rails ruby-on-rails-4

在我的代码中:

<%= form_for(:offer,url=>{:controller=>'offers',:action=>'combo'}) do |f|%>

<%= f.date_select :startDate %>

<%= f.date_select :endDate %>

我是rails的新手。我有两个字段来选择date.I希望endDate应该大于startDate.So,我如何验证这个endDate

2 个答案:

答案 0 :(得分:0)

在模态中添加以下行: -

validate :end_date_greater_than_start_date

def end_date_greater_than_start_date
  if !endDate.blank? and endDate.to_date < startDate.to_date
    errors.add(:endDate, "should be greater than start date")
  end
end

答案 1 :(得分:0)

无效,因为您为字段指定了非标准名称。请避免像这样使用camelCase和重构:

<%= f.date_field :start_date %>

<%= f.date_field :end_date %>

validate :end_date_greater_than_start_date

def end_date_greater_than_start_date
    if start_date && end_date && start_date > end_date
      errors.add(:to_date, "YOUR ERROR MESSAGE")
    end
end