我正在使用过滤效果好的宝石来过滤市场页面上的物品。
如果我为目录选择过滤器选项,则即使结果为空白,类型和颜色的选择选项仍然可见。如果结果为空,如何从选择中删除选项?
模型
catalog.rb
class Catalog < ApplicationRecord
belongs_to :vendor, optional: true
has_one :picture, as: :imageable
has_many :items
def self.options_for_select
order('LOWER(title)').map { |e| [e.title, e.id] }.reverse
end
end
color.rb
class Color < ApplicationRecord
has_many :items
def self.options_for_select
order('LOWER(title)').map { |e| [e.title, e.id] }.reverse
end
end
type.rb
class Type < ApplicationRecord
has_many :items
def self.options_for_select
order('LOWER(title)').map { |e| [e.title, e.id] }.reverse
end
end
item.rb
class Item < ApplicationRecord
belongs_to :catalog
belongs_to :color
belongs_to :type
filterrific(
default_filter_params: { sorted_by: 'price_at_asc' },
available_filters: [
:sorted_by,
:with_catalog_id,
:with_color_id,
:with_type_id
]
)
scope :with_catalog_id, lambda { |catalog_ids| where(catalog_id: [*catalog_ids]) }
scope :with_color_id, lambda { |color_ids| where(color_id: [*color_ids]) }
scope :with_type_id, lambda { |type_ids| where(type_id: [*type_ids]) }
scope :sorted_by, lambda { |sort_option|
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^price/
order("items.price #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
def self.options_for_sorted_by
[
['Price asc', 'price_asc'],
['Price desc', 'price_desc']
]
end
end
控制器
items_controller.rb
class ItemsController < ApplicationController
def show; end
def index
@filterrific = initialize_filterrific(Item, params[:filterrific],
select_options: {
sorted_by: Item.options_for_sorted_by,
with_catalog_id: Catalog.options_for_select,
with_type_id: Type.options_for_select,
with_color_id: Color.options_for_select},
persistence_id: 'shared_key',
default_filter_params: {}) || return
@obj = @filterrific.find
respond_to do |format|
format.html
format.js
end
end
end
查看
%h1 Items
.row
= render 'layouts/filter'
.filterrific_results{id: 'filterrific_results'}
= render 'items/listing', items: @obj
部分
过滤器
= form_for_filterrific @filterrific do |f|
.filter
Catalog
= f.select(:with_catalog_id, @filterrific.select_options[:with_catalog_id], class: 'filterrific-periodically-observed', include_blank: '- Any -')
.filter
Type
= console
= f.select(:with_type_id, @filterrific.select_options[:with_type_id].reject { |e| Type.find(e.last).items.count == 0}, class: 'filterrific-periodically-observed', include_blank: '- Any -')
.filter
Color
= f.select(:with_color_id, @filterrific.select_options[:with_color_id], class: 'filterrific-periodically-observed', include_blank: '- Any -')
.filter
Sort by price
= f.select(:sorted_by, @filterrific.select_options[:sorted_by], class: 'filterrific-periodically-observed', include_blank: '- Any -')
.filter
= link_to('Reset', reset_filterrific_url, class: 'btn btn-primary')
= render_filterrific_spinner
列出
- items.each do |item|
.col-xs-6.col-sm-6.col-md-4.col-lg-3
.thumbnail.hover-border
.caption.text-center
#{item.catalog.title} #{item.title} #{item.articul}
%br
#{item.price}