0
儘管我覺得我真的很接近解決方案,在我的應用程序中,您可以創建其他人可以回答的產品的搜索請求。現在我想要實現filterrific來對這些搜索請求進行排序(並且稍後還將篩選)。我跟着輝煌的文檔http://filterrific.clearcove.ca/,但我沒有找到它不適用於我的應用程序的原因。我沒有得到任何錯誤,但無論我做什麼都沒有排序。帶濾網的滑軌不起作用
這是我的搜索模式:
filterrific(
default_filter_params: { sorted_by: 'created_at_desc' },
available_filters: [
:sorted_by,
]
)
scope :sorted_by, lambda { |sort_option|
# extract the sort direction from the param value.
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^created_at_/
# Simple sort on the created_at column.
# Make sure to include the table name to avoid ambiguous column names.
# Joining on other tables is quite common in Filterrific, and almost
# every ActiveRecord table has a 'created_at' column.
order("searches.created_at #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
def self.options_for_sorted_by
[
['Created at (Newest first)', 'created_at_desc'],
['Created at (Oldest first)', 'created_at_asc']
]
end
這是我的搜索器:
def index
@filterrific = initialize_filterrific(
Search,
params[:filterrific],
select_options: {
sorted_by: Search.options_for_sorted_by
},
persistence_id: 'shared_key',
default_filter_params: {},
available_filters: [],
) or return
@searches = @filterrific.find.paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.html
format.js
end
end
這是我的index.html.erb:
<%= form_for_filterrific @filterrific do |f| %>
<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 %>
<%= render 'searches/search', locals: { searches: @searches } %>
這是我的部分_search.html.erb:
<div id="filterrific_results">
<% @searches.each do |search| %>
<%= time_ago_in_words(search.created_at) %>
...
<% end %>
</div>
<%= will_paginate @searches %>
最後,我index.js.erb的:
<% js = escape_javascript(
render(partial: 'searches/search', locals: { searches: @searches })
) %>
$("#filterrific_results").html("<%= js %>");
爲什麼我感覺更接近的解決方案? 將default_filter_params從'created_at_desc'更改爲'created_at_asc'正在工作。然後,選項選擇列表將自動更改爲「在(最早的最早)創建」。但是當我改變這個選項時,除了微調器出現一秒之外,沒有任何事情發生。
謝謝你的幫助!
當我禁用AJAX自動提交表單,並點擊提交它導致以下鏈接:? 搜索UTF8 =%E2%9C%93&filterrific%5Bsorted_by%5D = created_at_asc&提交=過濾 mabye,幫助查找問題!? – Eckenmeyer