2013-04-09 19 views
5

Hi używam ransack + kalendae_assets gem do wyszukiwania rekordów w między datą początkową a datą końcową tego używam PREDICATES ransack odwołując https://github.com/ernie/ransack/blob/master/lib/ransack/constants.rbszukają rekordy między datą rozpoczęcia i zakończenia dla ransack

tu jest mój Kod

<%= 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 

ale kiedy wejdzie rozpocząć d data końcowa nie wyszukuje, jak mogę to zrobić

+1

pokazać wyjście @ room.to_sql – Magnuss

+0

To pokazuje pusty wyświetlacz nic nic Choć istnieje wiele rekordów obecne w tym zakresie dat – r15

+0

@ room.to_sql powinien zwrócić kwerendy activerecord będzie używać do pobierania danych. Spróbuj "Room.search (parametry [: q]). Result.to_sql". – Magnuss

Odpowiedz

1

Możesz użyć zakresu z datą początkową i końcową. Następnie możesz uzyskać wyniki wyszukiwania między datami. Oto przykładowy kod w formularzu wyszukiwania:

<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 %>&nbsp;<%= 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> 

A oto kod kontrolera:

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? 

kod Helper:

#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 

Scenariusz:

$("#dates").click(function() { 
    var $that = $(this); 
    $("#range").toggle("slow", function() { 
    $that.toggleClass("toggled-off"); 
    }); 
}); 

Myślę, że to może ci pomóc ...

4

Z https://github.com/dangrossman/bootstrap-daterangepicker można zrobić datę zakres wyszukiwania z:

= 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() 
+0

Dzięki kolego, właśnie tego potrzebowałem. – KaizenCodes

3

Można zrobić własny predykat.

Moim zdaniem mam pole wyszukiwania ransack jak

= f.text_field :request_date_between, class: 'daterange' 

To wyśle ​​datę do sterownika jak

'2015-10-01 do 2015-10-31'

następnie w moim dół i brudny inicjator ransack.rb mam;

Ransack.configure do |config| 
    config.add_predicate 'between', 
         arel_predicate: 'between', 
         formatter: proc { |v| v.split(' to ') }, 
         type: :string 
end 

module Arel 
    module Predications 
    def between other 
     gteq(other[0]).and(lt(other[1])) 
    end 
    end 
end 
Powiązane problemy