Rails:Filterrific不更新范围。新范围不会在视野中加载

时间:2017-07-09 20:45:13

标签: jquery ruby-on-rails ruby scope filterrific

我正在尝试使用filterrific gem在我的索引页面上创建一个基本过滤器。不幸的是,filterrific似乎没有与我的数据库交谈。当我运行服务器并使用下拉菜单sort_by时,将呈现新的部分,但范围不会更改。

Started GET "/wikis?utf8=%E2%9C%93&filterrific%5Bsearch_query%5D=&filterrific%5Bsorted_by%5D=title_asc&_=1499629014459" for 127.0.0.1 at 2017-07-09 12:48:29 -0700
Processing by WikisController#index as JS
  Parameters: {"utf8"=>"✓", "filterrific"=>{"search_query"=>"", "sorted_by"=>"title_asc"}, "_"=>"1499629014459"}
  Wiki Load (0.5ms)  SELECT `wikis`.* FROM `wikis`
  Rendered wikis/_list.html.erb (4.2ms)
  Rendered wikis/index.js.erb (5.1ms)
Completed 200 OK in 8ms (Views: 6.9ms | ActiveRecord: 0.5ms)

同样,default_filter_params也不会影响显示的范围。

我定义的范围在控制台中完美运行,所以我确信它们是正确的。

这是我的模特

 class Wiki < ActiveRecord::Base
  belongs_to :user
  has_many :collaborators
  validates :body, presence: true
  validates :title,
    presence: true,
    uniqueness: { case_sensitve: false, scope: [:year]},
    length: { minimum: 3, maximum: 254}

  enum category: [:adventure, :dual_sport, :sport, :naked,
    :touring, :cruiser, :custom, :dirt, :roadster]

  enum final_drive: [:belt, :chain, :shaft]

  filterrific(
    default_filter_params: { sorted_by: 'title_asc'},
    available_filters: [
      :sorted_by,
      :search_query
    ]
  )

  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(students.first_name) LIKE ? OR LOWER(students.last_name) 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 /^title_/
      order("wikis.title #{ direction }")
    when /^cylinders_/
      order("wikis.cylinders #{ direction }")
    when /^hp_/
      order("wikis.hp #{ direction }")
    when /^torque_/
      order("wikis.torque #{ direction }")
    when /^displacement_/
      order("wikis.displacement #{ direction }")
    when /^year_/
      order("wikis.year #{ direction }")
    else
      raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
    end
  }

  def self.options_for_sorted_by
    [
      ['Title (a-z)', 'title_asc'],
      ['HP (most)', 'hp_desc'],
      ['HP (least)', 'hp_asc'],
      ['Torque (most)', 'torque_desc'],
      ['Torque (least)', 'torque_asc'],
      ['Year (oldest)', 'year_desc'],
      ['Year (newest)', 'year_asc'],
      ['Displacement (most)', 'displacement_desc'],
      ['Displacement (least)', 'displacement_asc'],
      ['Cylinders (most)', 'cylinders_desc'],
      ['Cylinders (least)', 'cylinders_asc'],
    ]
  end
end

我的控制器

...

def index
    @wikis = Wiki.all
    @filterrific = initialize_filterrific(
      Wiki,
      params[:filterrific],
      select_options: {
        sorted_by: Wiki.options_for_sorted_by,
      },
      available_filters: [:sorted_by]
    ) or return

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

我的观点

视图/维基/ index.html.erb

<div class="container">
  <div class="row">
    <div class="col-sm-8">
      <div>
        <%= form_for_filterrific @filterrific do |f| %>
        <div>
          Search
          <%= f.text_field(
            :search_query,
            class: 'filterrific-periodically-observed'
          ) %>
        </div>
        <div>
          Sorted by
          <%= f.select(:sorted_by, @filterrific.select_options[:sorted_by]) %>
        </div>
        <div>
          <%= link_to(
            'Reset filters',
            reset_filterrific_url,
          ) %>
        </div>
        <%= render_filterrific_spinner %>
        <% end %>

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

视图/维基/ _list.html.erb

<div id="filterrific_results">
  <% wikis.each do |wiki| %>
    <h4>
      <%= link_to wiki.title, wiki %>
      (<%= wiki.year %>)
    </h4>
  <% end %>
</div>

视图/维基/ index.js.erb的

<% js = escape_javascript(
  render(partial: 'wikis/list', locals: { wikis: @wikis })
) %>
$("#filterrific_results").html("<%= js %>");

我还在application.js

中加入了//= require filterrific/filterrific-jquery

我必须要有一些东西可以将它们捆绑在一起,但我可能会感到茫然。我最初使用sqlight3作为我的数据库,并认为这是问题所以我已经改为mysql2,因为它在filterrific文档中被列为支持。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

最后,我想出了我的错误。

在控制器中,当我将其定义为@wikis = Wiki.all时,我仍在定义@wikis = @filterrific.find。这样,filterrifc执行的搜索将存储在@wikis变量中。