复选框搜索Ransack gem

Lea*_*xxx 4 ruby-on-rails meta-search ransack ruby-on-rails-4

我需要过滤表格中的数据.为此,我找到了meta_search gem.我安装了meta_search,我收到此错误:

uninitialized constantActiveRecord::Associations::ClassMethods::JoinDependency
Run Code Online (Sandbox Code Playgroud)

对于Rails 4(我正在使用的框架),已被弃用.所以,我安装了ransack(rails 4 branch)(基于meta_search的gem)并且工作得很漂亮.问题是我需要使用meta_search的collection_checks方法来处理复选框,并且这种方法在搜索中不存在.所以,问题是:在ransack中有一个类似于collection_checks的方法来管理复选框吗?或者我该怎么做?

我有这个:

在此输入图像描述

我想过滤汽车,自行车或两者的行

Luk*_*yka 12

我不熟悉元搜索,但在Ransack(在Rails 4中)你可以使用这样的东西:

楷模:

class User < ActiveRecord::Base
  has_many :user_vehicles 
  has_many :vehicles, through: :user_vehicles
end

class UserVehicle < ActiveRecord::Base
  belongs_to :user
  belongs_to :vehicle
end

class Vehicle < ActiveRecord::Base
  has_many :user_vehicles
end
Run Code Online (Sandbox Code Playgroud)

控制器:

class UsersController < ApplicationController
  def index
    @q = User.search(params[:q])
    @users = @q.result(distinct: true)
  end
end
Run Code Online (Sandbox Code Playgroud)

视图:

<%= search_form_for @q , url: users_path, :html => { class: 'your-class' } do |f| %>
  <% Vehicle.all.each do |vehicle| %>
     <%= check_box_tag('q[vehicles_id_eq_any][]', vehicle.id ) %>
     <%= vehicle.name %>
  <% end %>
  <%= f.submit "Search" %>
<% end %>

<%= paginate(@users) %>
  <ul>
    <% @users.each do |i| %>
      <li><%= i.name %></li>
    <% end %>
  </ul>
<%= paginate(@users) %>
Run Code Online (Sandbox Code Playgroud)

如果您仍然不使用宝石,也可以使用宝石进行分页.我用Kaminari.在这种情况下,您的控制器和视图可能如下所示:

控制器:

class UsersController < ApplicationController
  def index
    @q = User.search(params[:q])
    @users = @q.result(distinct: true).page params[:page]
  end
end
Run Code Online (Sandbox Code Playgroud)

视图:

<%= search_form_for @q , url: users_path, :html => { class: 'your-class' } do |f| %>
  <% Vehicle.all.each do |vehicle| %>
     <%= check_box_tag('q[vehicles_id_eq_any][]', vehicle.id ) %>
     <%= vehicle.name %>
  <% end %>
  <%= f.submit "Search" %>
<% end %>

<%= paginate(@users) %>
  <ul>
    <% @users.each do |i| %>
      <li><%= i.name %></li>
    <% end %>
  </ul>
<%= paginate(@users) %>
Run Code Online (Sandbox Code Playgroud)