searching records between start date and end dates for ransack

r15 picture r15 · Apr 9, 2013 · Viewed 8.9k times · Source

Hi i am using ransack + kalendae_assets gem for searching records in between start date and end date for this i am using ransack PREDICATES by referring https://github.com/ernie/ransack/blob/master/lib/ransack/constants.rb

here is my code

<%= search_form_for @search, url: guest_search_rooms_path, html: {:method =>:post} do |f| %>
  <%= f.label :start_date_eq , "Start Date"%>   
  <%= f.text_field :start_date_eq, class: 'release_date' %>     

  <%=f.label :end_date_eq, "End Date" %>   
  <%= f.text_field :end_date_lteq, class: 'release_date' %>     
  <%= f.submit "search" %>
<% end %>

rooms.controller

def guest_search 

  @search = Room.search(params[:q])
  @roome = @search.result(:distinct => true)
  @room= @roome.where("status IS ?", true).order("room_type_id desc")

  #@room = @search.result(:distinct => true)
 end 

but when i enters start and end date it not searches how can i do this

Answer

tomaszbak picture tomaszbak · Jun 27, 2014

With https://github.com/dangrossman/bootstrap-daterangepicker you can do date range search with:

= search_form_for q, url: orders_path, builder: SimpleForm::FormBuilder do |f|
  = f.input :daterange , input_html: {value: "#{q.date_gteq.to_s} - #{q.date_lteq.to_s}"}
  = f.input :date_gteq, as: :hidden
  = f.input :date_lteq, as: :hidden

:coffee
  $ ->
    $('#q_daterange').daterangepicker
      format: "YYYY-MM-DD"
      startDate: $('#q_date_gteq').val()
      endDate: $('#q_date_lteq').val()
      ranges:
        'This Week': [moment().startOf('week'), moment().endOf('week')],
        'Next Week': [moment().add('week', 1).startOf('week'), moment().add('week', 1).endOf('week')]
    , (start, end, label) ->
      $('#q_date_gteq').val start.format("YYYY-MM-DD")
      $('#q_date_lteq').val end.format("YYYY-MM-DD")
    .on 'apply.daterangepicker', -> $('#order_search').submit()