Hi,
I am new to rails and filterrific. I started by setting only 1 filter sort_by and I can't get it to work. In view it does apply the default. And if I change the default the sorting changes when I refresh the page. But if I choose an option from sort dropdown it shows it's loading but then nothing change.
I noticed that if I change the default value only for example from name_asc to price_asc it throws an error Invalid sort options. But if I change both default and scope values to price
Note that I'm building the app on top of a database with capital column names.
class Product < ActiveRecord::Base
filterrific(
default_filter_params: { sorted_by: 'name_asc' },
available_filters: [
:sorted_by
]
)
belongs_to :category, :foreign_key => 'category'
belongs_to :extra1_detail, :foreign_key => 'extra1_id'
belongs_to :taxcategory, :foreign_key => 'taxcat'
scope :sorted_by, lambda { |sort_options|
direction = (sort_options =~ /desc$/) ? 'desc' : 'asc'
case sort_options.to_s
when /^name_/
order("products.name #{ direction }")
else
raise(ArgumentError, "Invalid sort option")
end
}
def self.options_for_sorted_by
[
['Name (AZ)', 'name_asc'],
['Name (ZA)', 'name_desc']
]
end
end
Controller
class ProductsController < ApplicationController
def index
@filterrific = initialize_filterrific(
Product,
params[:filterrific],
select_options: {
sorted_by: Product.options_for_sorted_by,
},
persistence_id: false,
default_filter_params: {},
available_filters: [],
) or return
@products = @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
# There is an issue with the persisted param_set. Reset it.
puts "Had to reset filterrific params: #{ e.message }"
redirect_to(reset_filterrific_url(format: :html)) and return
end
<div id="filterrific_results">
Hi, I am new to rails and filterrific. I started by setting only 1 filter sort_by and I can't get it to work. In view it does apply the default. And if I change the default the sorting changes when I refresh the page. But if I choose an option from sort dropdown it shows it's loading but then nothing change.
I noticed that if I change the default value only for example from name_asc to price_asc it throws an error Invalid sort options. But if I change both default and scope values to price
Note that I'm building the app on top of a database with capital column names.
rails (5.0.0.1) actioncable (= 5.0.0.1) actionmailer (= 5.0.0.1) actionpack (= 5.0.0.1) actionview (= 5.0.0.1) activejob (= 5.0.0.1) activemodel (= 5.0.0.1) activerecord (= 5.0.0.1) activesupport (= 5.0.0.1) bundler (>= 1.3.0, < 2.0) railties (= 5.0.0.1) sprockets-rails (>= 2.0.0)
filterrific (2.0.5) rails (>= 3.1.0)
ruby 2.3.1p112
Model Products
Controller
View List
View index
What could be the issue?