Filterrific gem没有按预期刷新或过滤我的表

时间:2016-12-17 07:27:12

标签: javascript ruby-on-rails ajax rubygems filterrific

我是rails的相对新手,我正在尝试使用filterrific gem进行一些过滤。基本上我有索引页面,显示供应商列表,我希望能够使用过滤来主动过滤/排序记录。我能够完成教程并完成所有错误,但由于某种原因,当您在表单中输入数据时,没有任何反应。根本没有令人耳目一新的观点。对我做错了什么的想法?

供应商模型:

    filterrific(
        default_filter_params: { sorted_by: 'created_at_desc' },
        available_filters: [
          :sorted_by,
          :search_query,
          :with_vendor_type_id,
          :with_created_at_gte
        ]
      )

    scope :search_query, lambda { |query|

      return nil  if query.blank?
      terms = query.downcase.split(/\s+/)

      terms = terms.map { |e|
        (e.gsub('*', '%') + '%').gsub(/%+/, '%')
      }

      num_or_conds = 2
      where(
        terms.map { |term|
          "(LOWER(vendors.name) LIKE ? OR LOWER(vendors.bio) LIKE ?)"
        }.join(' AND '),
        *terms.map { |e| [e] * num_or_conds }.flatten
      )
    }

    scope :sorted_by, lambda { |sort_option|

  direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
      case sort_option.to_s
      when /^created_at_/
        order("vendors.created_at #{ direction }")
      when /^name_/
        order("LOWER(vendors.name) #{ direction }, LOWER(students.bio) #{ direction }")
      when /^vendor_type_title_/
        order("LOWER(vnedor_types.title) #{ direction }").includes(:vendor_type)
      else
        raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
      end
    }

    scope :with_vendor_type_id, lambda { |vendor_type_ids|
      where(with_vendor_type_id: [*vendor_type_ids])
    }

    scope :created_at_gte, lambda { |reference_time|
      where('vendors.created_at >= ?', reference_time)
    }


  def self.options_for_sorted_by
    [
      ['Name (a-z)', 'name_asc'],
      ['Registration date (newest first)', 'created_at_desc'],
      ['Registration date (oldest first)', 'created_at_asc'],
      ['Type(a-z)', 'vendor_type_id_asc']
    ]
  end

我的供应商控制器

 def index

    @filterrific = initialize_filterrific(
      Vendor,
      params[:filterrific],
      select_options: {
        sorted_by: Vendor.options_for_sorted_by,
        with_vendor_type_id: VendorType.options_for_select
      },
      persistence_id: 'shared_key',
      default_filter_params: {},
      available_filters: [],
    ) or return

    @vendors = @filterrific.find.page(params[:page])

    # Respond to html for initial page load and to js for AJAX filter updates.
    respond_to do |format|
      format.html
      format.js
    end

  rescue ActiveRecord::RecordNotFound => e
    puts "Had to reset filterrific params: #{ e.message }"
    redirect_to(reset_filterrific_url(format: :html)) and return
  end

我的索引视图

<h1>Vendors</h1>

<%= form_for_filterrific @filterrific do |f| %>
  <div>
    Search
    <%= f.text_field(
      :search_query,
      class: 'filterrific-periodically-observed'
    ) %>
  </div>
  <div>
    Vendor Type
    <%= f.select(
      :with_vendor_type_id,
      @filterrific.select_options[:with_vendor_type_id],
      { include_blank: '- Any -' }
    ) %>
  </div>
  <div>
    Registered after
    <%= f.text_field(:with_created_at_gte, class: 'js-datepicker') %>
  </div>
  <div>
    Sorted by
    <%= f.select(:sorted_by, @filterrific.select_options[:sorted_by]) %>
  </div>
  <div>
    <%= link_to(
      'Reset filters',
      reset_filterrific_url,
    ) %>
  </div>
<% end %>

<%= render(
  partial: 'vendors/list',
  locals: { vendors: @vendors }
) %>

我的偏爱

<div id="filterrific_results">

  <div>
    <%= page_entries_info vendors %>
  </div>

  <table>
    <tr>
      <th>Name</th>
      <th>User ID</th>
      <th>Country</th>
      <th>Registered at</th>
    </tr>
    <% vendors.each do |vendor| %>
      <tr>
        <td><%= link_to(vendor.name, vendor_path(vendor)) %></td>
        <td><%= vendor.user_id %></td>
        <td><%= vendor.vendor_type %></td>
        <td><%= vendor.created_at %></td>
      </tr>
    <% end %>
  </table>
</div>

<%= will_paginate vendors %>

JS文件

<%# app/views/vendor/index.js.erb %>
<% js = escape_javascript(
  render(partial: 'vendors/list', locals: { vendors: @vendors })
) %>
$("#filterrific_results").html("<%= js %>");

我到达了渲染并通过所有错误消息的地步,但它实际上并没有进行过滤。

1 个答案:

答案 0 :(得分:1)

在'app / assets / javascripts / application.js'中加入//= require filterrific/filterrific-jquery