[英]Rails with Filterrific is not working
I don't get filterrific to work although I feel I am really close to a solution. 虽然我真的很接近解决方案,但我并没有工作的要求。 In my App you can create search requests for products which other people can answer.
在我的应用程序中,您可以为其他人可以回答的产品创建搜索请求。 Now I want to implement filterrific to sort (and later also filter) these search requests.
现在,我想实现filterrific来对这些搜索请求进行排序(并在以后进行过滤)。 I followed the brilliant documentation on http://filterrific.clearcove.ca/ but I do not find the reason why it is not working in my app.
我遵循了http://filterrific.clearcove.ca/上出色的文档,但是我找不到它无法在我的应用程序中工作的原因。 I don't get any errors but whatever I do it is not sorting.
我没有任何错误,但是我所做的一切都没有排序。
This is my Search model: 这是我的搜索模型:
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
This is my Search controller: 这是我的搜索控制器:
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
This is my index.html.erb: 这是我的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 } %>
This is my partial _search.html.erb: 这是我的_search.html.erb部分:
<div id="filterrific_results">
<% @searches.each do |search| %>
<%= time_ago_in_words(search.created_at) %>
...
<% end %>
</div>
<%= will_paginate @searches %>
And finally, my index.js.erb: 最后,我的index.js.erb:
<% js = escape_javascript(
render(partial: 'searches/search', locals: { searches: @searches })
) %>
$("#filterrific_results").html("<%= js %>");
Why I feel close to a solution? 为什么我感到接近解决方案? A change of the default_filter_params from 'created_at_desc' to 'created_at_asc' is working.
将default_filter_params从'created_at_desc'更改为'created_at_asc'是可行的。 And also the option select list is then automatically changing to 'created at (Oldest first)'.
然后,选项选择列表也会自动更改为“创建时间(最早(最早))”。 But when I change the option select nothing is happening except the spinner shows up for a second.
但是,当我更改选项时,除了微调器会显示一秒钟之外,什么也没有发生。
Thank you for any help! 感谢您的任何帮助!
I just found the solution myself. 我自己找到了解决方案。 I added
:sorted_by
to the Search controller. 我在搜索控制器中添加了
:sorted_by
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.