Rails表按日期过滤

时间:2015-08-12 13:35:33

标签: ruby-on-rails

我目前在rails中有一个标准表。问题是它像18000多行。每行都有一个由此<%= car.indate %><%= car.outdate %>

定义的开始日期和结束日期

我有一个日期选择器并将日期输入到文本框。哪个在这里;

<div class="well">
  <h3 class="title">Dates</h3>
  <%= f.input :start_date do %>
  <%= text_field_tag "search[date_searcher(1)]", @start, :class => "date_picker" %>
  <% end %>
  <%= f.input :end_date do %>
  <%= text_field_tag "search[date_searcher(2)]", @end, :class => "date_picker" %>
  <% end %>

  <p>To search using dates enter in Start date/End date then select the search method.</p>
</div>


<div class="row-fluid">
  <div class="well">
    <%= f.submit "Apply filters", :class => "btn btn-primary" %>
  </div>
</div>
<% end %>

如何让它编辑简单的html表只显示日期过滤器范围内的日期?

EDIT !!!

我在旧控制器的控制器中找到了这个,

params[:search] ||= {}
params[:search][:meta_sort] ||= "id.desc"
if params[:search]
  @start  =  params[:search]["date_searcher(1)"]
  @end    =  params[:search]["date_searcher(2)"]

  @option =  params[:search]["date_searcher(3)"]

还是不觉得那么近哈哈!

1 个答案:

答案 0 :(得分:1)

主要编辑

# Controller

def index
    @date_method = (params[:search].present? ? params[:search][:date_method] : 'indate').to_sym
    @start = selected_date(:start_date)
    @end = selected_date(:end_date)

    @cars = params[:search].present? ? Car.where(@date_method => @start..@end) : Car.none
end

private

def selected_date(symbol)
    params[:search].present? && params[:search][symbol].present? ? params[:search][symbol].to_date : Time.now.to_date
end

# index view

<%= form_for :search, method: :get do |f| -%>

    <%= f.select :date_method, options_for_select( [['In Date','indate'],['Out Date','outdate']], selected: @date_method ) -%>
    <%= f.date_field :start_date, value: @start -%>
    <%= f.date_field :end_date, value: @end -%>
    <%= f.submit 'Search' -%>

<% end -%>

<table>
    <thead>
        <tr>
            <th>Make</th>
            <th>Model</th>
            <th>Indate</th>
            <th>Outdate</th>
            <th colspan="3"></th>
        </tr>
    </thead>

    <tbody>
        <%= render(@cars) || content_tag(:tr, content_tag(:td, 'Select Dates To Find a Car', colspan:7)) %>
    </tbody>

</table>

部分:# _car.html.erb

<tr>
    <td><%= car.make %></td>
    <td><%= car.model %></td>
    <td><%= car.indate %></td>
    <td><%= car.outdate %></td>
    <td><%= link_to 'Show', car %></td>
    <td><%= link_to 'Edit', edit_car_path(car) %></td>
    <td><%= link_to 'Destroy', car, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>