我尝试在我的应用程序上实现Filterrific来过滤用户。我需要通过mother_tongue,locality和availability start_time进行过滤。但它不起作用。没有错误,但没有过滤...
模型user.rb:
filterrific(
default_filter_params: {},
available_filters: [
:search_query,
:search_query,
:with_start_time_gte
]
)
scope :search_query, lambda { |query|
return nil if query.blank?
terms = query.downcase.split(/\s+/)
terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
num_or_conditions = 2
where(
terms.map {
or_clauses = [
"LOWER(users.mother_tongue) LIKE ?",
"LOWER(users.locality) LIKE ?"
].join(' OR ')
"(#{ or_clauses })"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conditions }.flatten
)
}
scope :with_start_time_gte, lambda { |ref_date|
where('availability.start_time >= ?', ref_date)
}
Users_controller.rb:
def index
@filterrific = initialize_filterrific(
User,
params[:filterrific]
#persistence_id: 'shared_key',
#default_filter_params: {},
#available_filters: [],
) or return
@users = @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
index.html.erb:
<% provide(:title, 'Liste des utilisateurs') %>
<h1>Liste des utilisateurs :</h1>
<%= form_for_filterrific @filterrific, html: { id: 'filterrific-no-ajax-auto-submit' } do |f| %>
<div>
Langue :
<%= f.text_field(
:search_query,
class: 'mother_tongue filterrific-periodically-observed'
) %>
</div>
<div>
Ville :
<%= f.text_field(
:search_query,
class: 'locality filterrific-periodically-observed'
) %>
</div>
<div>
Date de départ :
<%= f.text_field(:with_start_time_gte, class: 'js-datepicker') %>
</div>
<%= f.submit 'Rechercher' %>
<div>
<%= link_to(
'Reset filters',
reset_filterrific_url,
) %>
</div>
<%= render_filterrific_spinner %>
_list.html.erb:
<div id="filterrific_results">
<div>
<%= page_entries_info users %>
</div>
<% @users.each do |user| %>
<h1><%= user.fname %> <%= user.lname %></h1>
<h2><%= user.mother_tongue %></h2>
<center>
<p><%= user.postal_code %> <%= user.locality %></p>
<p><%= link_to "En savoir plus", user_path(user) %></p>
</center>
<% end %>
我是铁杆上的新人,谢谢你的帮助。
答案 0 :(得分:0)
您使用的方法scope :search_query...
用于在您指定的2个数据库列之一users.mother_tongue
和users.locality
中查找输入文本。事实上,结果where子句将是"WHERE LOWER(users.mother_tongue) LIKE ? OR LOWER(users.locality) LIKE ?"
。
如果要单独过滤2个字段的值,则必须定义2个具有不同名称的输入字段,2个过滤器和2个范围。
代码可能是这样的:
模型user.rb:
filterrific(
default_filter_params: {},
available_filters: [
:with_mother_tongue,
:with_locality,
:with_start_time_gte
]
)
scope :with_mother_tongue, -> (search_string) {
where("users.mother_tongue ILIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}
scope :with_locality, -> (search_string) {
where("users.locality ILIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}
scope :with_start_time_gte, lambda { |ref_date|
where('availability.start_time >= ?', ref_date)
}
在索引视图中,您还必须呈现_list部分(请参见末尾):
index.html.erb:
<% provide(:title, 'Liste des utilisateurs') %>
<h1>Liste des utilisateurs :</h1>
<%= form_for_filterrific @filterrific, html: { id: 'filterrific-no-ajax-auto-submit' } do |f| %>
<div>
Langue :
<%= f.text_field(
:with_mother_tongue,
class: 'mother_tongue filterrific-periodically-observed'
) %>
</div>
<div>
Ville :
<%= f.text_field(
:with_locality,
class: 'locality filterrific-periodically-observed'
) %>
</div>
<div>
Date de départ :
<%= f.text_field(:with_start_time_gte, class: 'js-datepicker') %>
</div>
<%= f.submit 'Rechercher' %>
<div>
<%= link_to(
'Reset filters',
reset_filterrific_url,
) %>
</div>
<%= render_filterrific_spinner %>
#filterrific_results
= render 'users/list', users: @users
最后,当您过滤填充至少一个过滤器输入字段的记录时,您必须定义控制器中索引操作调用的js视图:
index.js.erb的
<% js = escape_javascript (
render(partial: 'users/list', locals: {users: @users})
) %>
$("#filterrific_results").html("<%= js %>");