简体   繁体   English

Rails select2含极好的宝石

[英]Rails select2 with filterrific gem

I currently have a list of events that I want my users to be able to filter. 我目前有一个事件列表,希望我的用户能够进行过滤。 Each event has a performer and I store the performers id for each event. 每个事件都有一个表演者,我存储每个事件的表演者ID。

It isn't very user friendly for my users to be able to search by the user id as it won't mean much to them so I want them to be able to search by the performers name. 我的用户能够通过用户ID搜索不是非常用户友好,因为这对他们来说意义不大,所以我希望他们能够通过表演者姓名进行搜索。

I had previously created a simple search form that used select2 rails gem and the code in my view would look something like this: 我以前创建了一个简单的搜索表单,该表单使用了select2 rails gem,并且视图中的代码如下所示:

<%= select_tag(:performer, options_from_collection_for_select(@performer,
              :id, :profile_name, :selected => params[:performer]), :include_blank => true, id: "user", :class => "form-control")  %>

<script type="text/javascript">
$(document).ready(function() {
$('select#user').select2({
  allowClear: true,
  placeholder: "Choose a profile name",
});

}); });

This would allow my users to be able to search and select a name and it would find the relevant events that related to their user_id. 这将使我的用户能够搜索和选择一个名称,并找到与他们的user_id相关的事件。

I now want to use the filterrifc gem to make the search update in realtime as the user is typing and to display the results but I cannot work out how to modify my code to work alongside the gem. 现在,我想使用filterrifc gem在用户键入内容时实时更新搜索并显示结果,但是我无法确定如何修改代码以与gem一起使用。

If I follow the example in the documentation for a simple text search, the scope in the model would look like this: 如果我按照文档中的示例进行简单的文本搜索,则模型中的范围如下所示:

   scope :performer, lambda { |query|
where("LOWER(events.performer) LIKE ?", "%#{query}%")}

But I cannot work out how to combine the select2 gem with the filterrific gem to get the result that I want. 但是我无法解决如何将select2 gem与filterrific gem结合起来以获得我想要的结果。

这似乎已经解决了问题。

<%= f.select :performer, options_from_collection_for_select(@performer, :profile_name,:name), {:include_blank => true}, id: "user", class: "form-control", data: { placeholder: "Choose a performer" } %> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM