How to get a group of conditions in Ransack?

I am using advanced search in Ransack, which has a default value of AND. I did this as OR by putting .try (: merge, m: 'or'))

@search = Data.search(params[:q].try(:merge, m: 'or')) 

but I cannot remove AND / OR as a group of conditions, as shown in the Ransack demo here http://ransack-demo.herokuapp.com/users/advanced_search p>

enter image description here

How to do this, unfortunately, the Ransack wiki does not mention this.

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 !@pids.nil ? %> <div class="total_count"><b><%= @pids.count %> Records Found.</b></div> <% end %> 
+5
source share
1 answer

If you check the extended demo, you will find that this m: 'or' is m: 'or' placed in the hashes of the condition group directly in the q parameter.

The easiest way to achieve this is to simply add a hidden field to group such fields

 <%= 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 the parameters are sent to the demo.

enter image description here

as you can see from the image merge parameter, is under the g parameter

Note If you need custom values ​​for any all words checkout Translate module .

As you can see, the world method returns I18n.translate , so in this case you can simply put the user values ​​in en.yml under the ransack.all and ransack.all .

+3
source

Source: https://habr.com/ru/post/1261381/


All Articles