You can use range with starting date and end date. Then you can get search result between the dates. Here is the sample code in your search form:
<div class="control-group">
<%= f.label :scrap_date_cont, "Scrap Date", class: 'control-label' %>
<div class="controls">
<% if q.scrap_date_cont.blank? %>
<%= f.text_field :scrap_date_cont, include_blank: true, default: nil, :class => 'datepicker3', :style=>"width:100px;" %>
<% elsif !q.scrap_date_cont.blank? %>
<%= f.text_field :scrap_date_cont, :value => "#{change_date_format_for_edit_page(q.scrap_date_cont)}", :class => 'datepicker3', :style=>"width:100px;" %>
<% end %> <%= link_to "Select Range", "#", :id => 'dates' %>
</div>
</div>
<div class="control-group" id="range" style="display:none" >
<%= f.label :scrap_date_gteq, "Range", class: 'control-label' %>
<div class="controls">
<% if q.scrap_date_gteq.blank? %>
<%= f.text_field :scrap_date_gteq, include_blank: true, default: nil, :class => 'datepicker1', :style=>"width:100px;" %>
<% elsif !q.scrap_date_gteq.blank? %>
<%= f.text_field :scrap_date_gteq, :value => "#{change_date_format_for_edit_page(q.scrap_date_gteq)}", :class => 'datepicker1', :style=>"width:100px;" %>
<% end %>
<% if q.scrap_date_lteq.blank? %>
<%= f.text_field :scrap_date_lteq, include_blank: true, default: nil, :class => 'datepicker2', :style=>"width:100px;" %>
<% elsif !q.scrap_date_lteq.blank? %>
<%= f.text_field :scrap_date_lteq, :value => "#{change_date_format_for_edit_page(q.scrap_date_lteq)}", :class => 'datepicker2', :style=>"width:100px;" %>
<% end %>
</div>
</div>
And Here is the controller code:
params[:q][:scrap_date_cont] = change_date_format(params[:q][:scrap_date_cont]) if !(params[:q][:scrap_date_cont]).blank?
params[:q][:scrap_date_cont] = params[:q][:scrap_date_cont].to_date.strftime("%d/%Y/%m") if !(params[:q][:scrap_date_cont]).blank?
params[:q][:scrap_date_gteq] = change_date_format(params[:q][:scrap_date_gteq]) if !(params[:q][:scrap_date_gteq]).blank?
params[:q][:scrap_date_gteq] = params[:q][:scrap_date_gteq].to_date.strftime("%d/%Y/%m") if !(params[:q][:scrap_date_gteq]).blank?
params[:q][:scrap_date_lteq] = change_date_format(params[:q][:scrap_date_lteq]) if !(params[:q][:scrap_date_lteq]).blank?
params[:q][:scrap_date_lteq] = params[:q][:scrap_date_lteq].to_date.strftime("%d/%Y/%m") if !(params[:q][:scrap_date_lteq]).blank?
Helper code:
#Change date format in edit time
def change_date_format_for_edit_page(date)
new_date = date.strftime("%m/%d/%Y")
puts new_date.inspect
return new_date
end
Script :
$("#dates").click(function () {
var $that = $(this);
$("#range").toggle("slow", function() {
$that.toggleClass("toggled-off");
});
});
I think it might help you...
<%= f.text_field :start_date_eq, class: 'release_date' %>
you should write<%= f.text_field :start_date_gteq, class: 'release_date' %>
. – Dittman