'How to filter status with Rails Ransack's filter?
I found a description in the Ranksack tutorial (https://activerecord-hackery.github.io/ransack/getting-started/sorting) about filters. I have used Ransack to complete the search and sorting functions. I hope to add a filter to let the user select the desired state (this is one of the columns in the table) and point out the data that matches the state. But I don't know how to transform this code. Please give me some hints, thanks.
This is the code mentioned in the tutorial
<div class="filters" id="filtersSidebar">
<header class="filters-header">
<div class="filters-header-content">
<h3>Filters</h3>
</div>
</header>
<div class="filters-content">
<%= search_form_for @q,
class: 'form',
url: articles_path,
html: { autocomplete: 'off', autocapitalize: 'none' } do |f| %>
<div class="form-group">
<%= f.label :title_cont, t('Filter_by_keyword') %>
<%= f.search_field :title_cont %>
</div>
<%= render partial: 'filters/date_title_sort', locals: { f: f } %>
<div class="form-group">
<%= f.label :grade_level_gteq, t('Grade_level') %> >=
<%= f.search_field :grade_level_gteq %>
</div>
<div class="form-group">
<%= f.label :readability_gteq, t('Readability') %> >=
<%= f.search_field :readability_gteq %>
</div>
<div class="form-group">
<i><%= @articles.total_count %> articles</i>
</div>
<div class="form-group">
<hr/>
<div class="filters-header-content">
<%= link_to request.path, class: 'form-link' do %>
<i class="far fa-undo icon-l"></i><%= t('Clear_all') %>
<% end %>
<%= f.submit t('Filter'), class: 'btn btn-primary' %>
</div>
</div>
<% end %>
</div>
</div>
This is the current code of my page
<%= search_form_for(@query, url: list_tasks_path) do |f| %>
<%= f.label :name, t('search') %>
<%= f.search_field :name_or_note_cont, placeholder: t('task_name') %>
<%= f.select :status_eq, Task.statuses.map { |key, value| [t("checkpoint_status.#{key}"), value] }, prompt: t('all_status') %>
<%= f.submit %>
<% end %>
<p class="lead"><%= t('task_list') %></p>
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">
<%= sort_link(@query, :name) do %>
<%= t('task_name') %>
<% end %>
</th>
<th scope="col">
<%= sort_link(@query, :status) do %>
<%= t('task_status') %>
<% end %>
</th>
<th scope="col">
<%= sort_link(@query, :priority) do %>
<%= t('task_priority') %>
<% end %>
</th>
<th scope="col">
<%= sort_link(@query, :created_at) do %>
<%= t('task_created') %>
<% end %>
</th>
<th scope="col">
<%= sort_link(@query, :due_date) do %>
<%= t('task_ends') %>
<% end %>
</th>
<th scope="col"><%= t('task_actions') %></th>
<th colspan="2"></th>
<th scope="col"><%= t('task_tag') %></th>
</tr>
</thead>
<tbody>
<% @tasks.each do |task| %>
<tr>
<th scope="row"><%= task.id %></th>
<td>
<%= task.name %>
</td>
<td>
<%= t("checkpoint_status.#{task.status}") %>
</td>
<td>
<%= t("checkpoint_priority.#{task.priority}") %>
</td>
<td>
<%= task.created_at.strftime("%b %d %R") %>
</td>
<td>
<%= task.due_date.strftime("%b %d %R") %>
</td>
<td>
<%= link_to t('task_cont') , task_path(task) %>
</td>
<td>
<%= link_to t('task_do_edit') , edit_task_path(task) %>
</td>
<td>
<%= link_to t('task_do_kill') , task_path(task), method: :delete, data:{ confirm: t('task_do_sure') } %>
</td>
<td>
<%= task.tag_list %>
</td>
</tr>
<% end %>
</tbody>
</table>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
