为 Rails、smart_listing gem 上的过滤器查询创建一个 like 方法

Create a like method for filter query on Rails, smart_listing gem

我想过滤我与 smart_listing gem

创建的 table 的内容

对于未知的,SmartListing 也称为 smart_listing 是一个 Ruby Gem,它提供了一个工具在您的 Rails 应用程序

上创建列表

我可以使用分页,但过滤器仍然不起作用

Link to smart_listing documentation for Controls (filtering) documentation section

在文档中说:

# Apply the search control filter.
  # Note: `like` method here is not built-in Rails scope. You need to define it by yourself.
  users_scope = users_scope.like(params[:filter]) if params[:filter]

这是我的代码,我如何运行过滤器?我在 controller

上有这个
if !current_user.current_organization.import_columns.nil?
      @import_contacts = UserImport.where(organization_id: current_user.current_organization.id)

      @import_contacts_column_filter = current_user.current_organization.import_columns.split(/,/)
      contacts_import_scope = @import_contacts
      contacts_import_scope = contacts_import_scope.like(params[:filter]) if params[:filter]
      #contacts_import_scope = begin_of_association_chain.order('active DESC')
      #contacts_import_scope = contacts_import_scope.imported_users_filter(params[:filter].strip) if params[:filter]
      @import_contacts_listing = smart_listing_create(
        :import_contacts,
        contacts_import_scope,
        partial: 'contacts/listing_import',
        default_sort: {created_at: "desc"}
      )
    end

这个浏览量

index.html.slim

.tab-content
      - if @import_contacts.any?
        .search-wrapper.pull-right
          = smart_listing_controls_for(:import_contacts) do
            span.btn.pull-right
              i.fa.fa-search
            .filter.input-append.pull-right
              = text_field_tag :filter, '', class: 'search', placeholder: "#{t('commons.filters.search_pending_petition_contacts')}", autocomplete: 'off'

_listing_import.slim

- unless smart_listing.empty?
  .pending_contacts_table.table-responsive
    table class="table-striped table-list" style="width: 100%; background-color: #FAFBFD;"
      thead
        tr
          - @import_contacts_column_filter.each do |column|
            th.name.col-sm-2 = smart_listing.sortable "#{column}", "#{column}"
          th.name.col-sm-2 Añadir
      tbody
        - smart_listing.collection.each do |user_imported|
            tr
              - @import_contacts_column_filter.each do |field|
                td = "#{user_imported.send(field)}"
              td
                - if User.exists?(email: user_imported.email)
                  p Ya añadido
                - else
                  - last_name = "#{user_imported.surname1}"+"#{user_imported.surname2}"
                  = link_to new_organization_customer_path(name: user_imported.name, last_name: last_name, email: user_imported.email, tmp_pass: user_imported.tmp_pass), class: 'btn btn-success' do
                    i class='fa fa-plus-square' 
    = page_entries_info smart_listing.collection
    = smart_listing.paginate
- else

我们如何定义文档的 .like 方法?

正如我写在代码上并留下评论,在我尝试以这种方式编写之前也没有结果。

#contacts_import_scope = contacts_import_scope.imported_users_filter(params[:filter].strip) if params[:filter]

已解决!

问题出在 ImportsController,必须在其上定义,而不是在 ContactsController 上定义。

控制器也最终看起来像这样

def load_import_contacts 
    @import_contacts_column_filter = current_user.current_organization.import_columns.split(/,/) 
    @import_contacts = UserImport.where(organization_id: current_user.current_organization.id) 
    @import_contacts = @import_contacts.where("name Like ? OR surname1 LIKE ? OR surname2 LIKE ? OR reference_number LIKE ? OR email LIKE ? OR mutua LIKE ?", "%#{params[:filter]}%","%#{params[:filter]}%", "%#{params[:filter]}%", "%#{params[:filter]}%","%#{params[:filter]}%", "%#{params[:filter]}%") if params[:filter] 
    @import_contacts_listing = smart_listing_create( 
      :import_contacts, 
      @import_contacts,

_listing_import.slim...

@import_contacts = @import_contacts.where("name Like ? OR surname1 LIKE ? OR surname2 LIKE ? OR reference_number LIKE ? OR email LIKE ? OR mutua LIKE ?", "%#{params[:filter]}%","%#{params[:filter]}%", "%#{params[:filter]}%", "%#{params[:filter]}%","%#{params[:filter]}%", "%#{params[:filter]}%") if params[:filter] 

终于,过滤和分页成功了!