I use Rails 5.1 and the current version of the ransack gem.
I have a "statistics" page where I show jobs and I'd like to implement a filter functionality with two dropdown select fields, 1) years (2011-2018) and 2) months (jan-dec). How can I do that?
I was thinking of something like
# in statistics index view
<%= search_form_for @search do |f| %>
<%= f.grouping_fields(f.object.new_grouping) do |g| %>
<%= g.condition_fields(g.object.new_condition) do |c| %>
<%= # here the years dropdown list%>
<%= # here the months dropdown list%>
<% end %>
<% end %>
<% end %>
<%= f.submit "filter" %>
<% end %>
How should it look like correctly?
I'm not sure if it can be done by ransack or not, optionally you can do this by using simple form :- like this: -
<%= form_tag jobs_path, method: :get%>
<%= select_year(Date.today, {:prompt => "Year",:start_year => DateTime.now.year,:end_year => DateTime.now.year - 7, prefix: 'start_date'},{:field_name => 'year', :id => 'start-year'}) %>
<%= select_month(Date.today, {:prompt => "month",use_short_month: true, prefix: 'start_date'},{:field_name => 'month', :id => 'start-month'}) %>
<%= submit_tag("Search Job", :id=>"button", :class=>"Test", :name=>"submit") %>
<%end%>
and in your index action
def index
if params[:year].present? or params[:month].present?
#@jobs = Job.where("colum BETWEEN ? AND ?", ...)
#@jobs = your_own_query_for_filter
else
@jobs = Job.all
end
end
i haven't written code properly, its just an example that you can do in this way also. hope it will help you.
Find here for date select form helper