尽管我觉得我已经接近解决方案了,但我还是不能很好地工作。在我的应用程序中,您可以为其他人可以回答的产品创建搜索请求。现在,我想实现filter来对这些搜索请求进行排序(稍后也会过滤)。我遵循了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”正在运行。并且选项选择列表然后被自动地改变为“创建于(最早的最先)”。但是,当我改变选项选择时,除了微调器显示一秒钟之外,什么也没有发生。
谢谢你的帮助!
发布于 2016-07-25 03:12:16
我自己找到了解决方案。我将:sorted_by添加到搜索控制器中。
https://stackoverflow.com/questions/38551116
复制相似问题