How to get Condition Group in Ransack?
Asked Answered
O

1

7

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

enter image description here

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 %>
Outfall answered 16/12, 2016 at 10:44 Comment(3)
can you show your form?Hospodar
can you also provide the exact error that you are getting?Hospodar
@Hospodar - I have updated the question with full source code. This code works but it doen't show the AND/OR option called Condition Groups (as shown in Ransack Demo) which we need.Outfall
O
4

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

enter image description here

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.

Oribelle answered 26/12, 2016 at 9:28 Comment(4)
I actually have made some progress after posting the question and was able to get the drop down with f.combinator_select. Please check the current code #41297930. Can you check that code as it seems now the issue is nested form in rails. I will accept this answer too if I can get that work (and will put the working code in question)Outfall
@Outfall just check the code I left in the answer, it's using grouping_fields which seems you are missing in your codeOribelle
unfortunately the above code didn't showed the drop down. It is not giving any error, just submitting the OR silently.Outfall
ah I got it, you need a selector I thought you need just force OR merging. Here is a helper for it github.com/activerecord-hackery/ransack/blob/master/lib/ransack/… Also checkout updated code in answerOribelle

© 2022 - 2024 — McMap. All rights reserved.