感謝大家的建議。這是我最終的解決方案。
首先,確保只有有效的日期可以通過使用datepicker在窗體中輸入,如下所示...
$(function(){
$('#startdate').datepicker({
changeYear: true,
dateFormat:"dd/mm/yy",
defaultDate: "#{Date.today.day}/#{Date.today.month}/#{Date.today.year}",
change: function() {
var isoDate = this.getValue('yyyy-mm-dd');
$(this).attr('data-value', isoDate);
}
});
$('#enddate').datepicker({
changeYear: true,
dateFormat:"dd/mm/yy",
defaultDate: "#{Date.today.day}/#{Date.today.month}/#{Date.today.year}",
change: function() {
var isoDate = this.getValue('yyyy-mm-dd');
$(this).attr('data-value', isoDate);
}
});
});
%form{:action => employee_results_path(@employee), :class => 'navbar-search pull-left'}
.field
%input{:type =>'text', :placeholder => params[:startdate] != nil ? params[:startdate] : "from date", :id => 'startdate', :name => "startdate", :style => 'width: 100px'}
%input{:type =>'text', :placeholder => params[:startdate] != nil ? params[:enddate] : "to date", :id => 'enddate', :name => "enddate", :style => 'width: 100px'}
然後,控制器採集的參數變量,分析它們的日期
sdate = Date.parse(params[:startdate])
edate = Date.parse(params[:enddate])
然後用有用的解決方案我在這個問題
@results = Result.where(:date => sdate..edate)
其工作一種享受收到。感謝大家幫助我學習!
您的日期輸入格式是否始終是固定的? –