I am trying to filter the records by date, however I am not sure what I could be missing since although the obtained parameter appears in the url, it does not filter anything, it does not show any item, below I share my code:
clients_controller.rb
class ClientsController < ApplicationController
def index
if params[:created_at].present?
@clients = Client.where(created_at: params[:created_at])
else
@clients = Client.all
end
end
end
index.html.erb
<%= form_tag({ controller: :clients, action: :index }, { method: :get } ) do %>
<%= date_field_tag :created_at %>
<%= submit_tag "Filtrar", name: nil %>
<% end %>
<table>
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Service</th>
<th>Note</th>
<th>Check in</th>
<th>Delivery</th>
<th>Delivered</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @clients.each do |client| %>
<tr>
<td><%= client.name %></td>
<td><%= client.phone %></td>
<td><%= client.service.name %></td>
<td><%= client.note %></td>
<td><%= client.check_in %></td>
<td><%= client.delivery %></td>
<td><%= client.delivered %></td>
<td><%= link_to 'Show', client %></td>
<td><%= link_to 'Edit', edit_client_path(client) %></td>
<td><%= link_to 'Destroy', client, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'New Client', new_client_path %>
You should query the database for records created in a time range rather than on a specific date.
For example, if you have as date
2019-02-07
, in your case you perform the following query:On the other hand, if you parse that date that comes in a string and convert it to a
Date
ruby object, you can easily obtain the start/end ranges of that day: