I am using advanced search in Ransack which has default AND. I have made it as OR by putting .try(:merge, m: 'or'))
@search = Data.search(params[:q].try(:merge, m: 'or'))
but I am not able to get the AND/OR drop down as Condition Group like shown in Ransack Demo here http://ransack-demo.herokuapp.com/users/advanced_search
How to do it as unfortunately Ransack wiki has no mention about it.
OUR CODE
data_controller.rb
def search
@search = Data.search(params[:q])
@datum = @search.result(:distinct=>true).paginate(page: params[:page], per_page: 10)
if params[:q].nil?
@datum = Prospect.where(:id => 0).paginate(page: params[:page], per_page: 10)
end
@page = params[:page] || 0
@pids = @search.result(:distinct=>true).pluck(:id)
@search.build_condition
# @search.build_grouping unless @search.groupings.any? (I have tried this code too but this gives an error)
end
routes.rb
resources :data do
collection do
get :search
post :search, to: 'data#search'
end
end
data.html.erb
<script type="text/javascript">
var ids = <%= @pids %>;
</script>
<section class="psf">
<div class="container">
<h1>All Data</h1>
<div class="row">
<div class="col-lg-10">
<div class="form_search">
<%= search_form_for @search, url: search_data_index_path, html: { method: :get, class: "data_search" } do |f| %>
<%= f.condition_fields do |c| %>
<%= render "condition_fields", f: c %>
<% end %>
<p><%= link_to_add_fields "Add Conditions", f, :condition %></p>
<br>
<div class="actions">
<%= f.submit "Search", :class => "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
<br>
<% if [email protected]? %>
<div class="total_count"><b><%= @pids.count %> Records Found.</b></div>
<% end %>
If you check the advanced demo you'll find that this m: 'or'
is placed into condition group hash not directly into the q
parameter.
The easiest way to achieve it is just add a hidden field to group fields like so
<%= search_form_for @search, url: search_data_index_path, html: { method: :get, class: "data_search" } do |f| %>
<%= f.grouping_fields do |g| %>
<%= g.combinator_select %>
<%= g.condition_fields do |c| %>
<%= render "condition_fields", f: c %>
<% end %>
<% end %>
<p><%= link_to_add_fields "Add Conditions", f, :condition %></p>
<br>
<div class="actions">
<%= f.submit "Search", :class => "btn btn-primary" %>
</div>
<% end %>
Also here you can check how parameters are sent in the demo
as you can see from picture merge parameter is under g
parameter
NOTE
If you want custom values for any
all
words checkout Translate module
.
As you can see world
method fallbacks to I18n.translate
so in this case you can just put custom values into en.yml
under ransack.all
and ransack.all
keys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With