vendredi 23 décembre 2016

How to get Conditional Groups in Ransack?

Using Railscast and Ransack demo code, I am able to build Advanced Search like this

enter image description here

The drop down "all/any" comes from <%= f.combinator_select %> and it works but I need Conditional Groups with (X AND Y) OR (A AND B) AND (M OR N) etc which I am not able to get.

I saw the Ransack Demo example multiple times but it is using Rails 5 and I am not very clear with some part of it.

Here is my code in Rails 4, can you tell me how to get Conditional Grouping ?

routes.rb

resources :data do
  collection do
    get :search
    post :search, to: 'data#search'
  end
end

data.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
    end

search.html.erb

<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| %>
            <%= f.combinator_select %>
            <%= 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>

_condition_fields.html.erb

<div class="field">
    <%= f.attribute_fields do |a| %>
    <%= a.attribute_select associations: [:user, :data] %>
    <% end %>
    <%= f.predicate_select %>
    <%= f.value_fields do |v| %>
    <%= v.text_field :value %>
    <% end %>
    <%= link_to "remove", '#', class: "remove_fields" %>
</div>

Aucun commentaire:

Enregistrer un commentaire