您好我使用Filterrific Gem为我的用户页面添加搜索和过滤功能。
搜索工作正常,但当我尝试实现“排序依据”过滤时,我得到了undefined method
[]'nil:NilClass`错误。
我在这里引用了Filterrific文档:http://filterrific.clearcove.ca/pages/action_view_api.html
以下是我的代码:
index.html.erb
<div class="well">
<%= form_for_filterrific @filterrific do |f| %>
<div>
<%= f.text_field( :search_query, id: "filterrific-no-ajax-auto-submit" ,class: 'form-control', placeholder: "Search users...") %>
</div>
<div>
Sorted by
<% f.select(:sorted_by, @filterrific.select_options[:sorted_by]) %>
</div>
<% end %>
</div>
<div class="row">
<%= render(
partial: 'users/list',
locals: { users: @users }
) %>
</div>
model.rb
filterrific(
default_filter_params: { sorted_by: 'first_name_asc' },
available_filters: [
:sorted_by,
:search_query,
:with_created_at
]
)
scope :sorted_by, lambda { |sort_option|
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^first_name/
order("users.first_name #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
users_controller.rb
def index
@filterrific = initialize_filterrific( User, params[:filterrific] ) or return
@users = @filterrific.find.page(params[:page]).order('first_name ASC')
respond_to do |format|
format.html
format.js
end
end
以下是错误的屏幕截图:
请帮忙。谢谢!
答案 0 :(得分:0)
在控制器中初始化时。参数不包含select_options。它们应包含select_options。现在它看起来像:
&#34; filterrific&#34; = GT; {&#34; SEARCH_QUERY&#34; = GT;&#34;&#34;}
它应该像
&#34; filterrific&#34; =&gt; {&#34; search_query&#34; =&gt;&#34;&#34;,&#34; select_options&#34; =&gt; YOUR-OPTIONS}
答案 1 :(得分:0)
在您的用户控制器中
您需要设置控制器以接受您的选择选项和您将需要的其他参数,就像with_created_at
一样。
def index
@filterrific = initialize_filterrific(
User,
params[:filterrific],
select_options: {
:sorted_by => User.options_for_sorted_by
}
) or return
@users = @filterrific.find.page(params[:page])
respond_to do |format|
format.html
format.json
end
end
在您的用户模型中
您还需要将您在控制器中调用的方法写入模型,并且不需要像特定过滤器一样指定with_created_at
,它可以在options for sorted by
中就像上面的例子一样。
scope :sorted_by, lambda { |sort_option|
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^first_name/
order("users.first_name #{ direction }")
when /^last_name/
order("users.last_name #{ direction }")
when /^created_at/
order("LOWER(users.created_at) #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
def self.options_for_sorted_by
[
['First Name (a-z)', 'first_name_asc'],
['Last Name (a-z)', 'last_name_asc'],
['Created at (newer first)', 'created_at_desc'],
['Created at (older first)', 'created_at_asc'],
]
end
最后但同样重要的是,您必须设置作用域以使search_query
能够按需工作。您可以像这样定义特定过滤器的范围。
filterrific(
default_filter_params: { sorted_by: 'first_name_asc' },
available_filters: [
:sorted_by,
:search_query, # defining a search query to find in the db fields
:school # defining a scope to a foreign key
]
)
# here i setup the search query
scope :search_query, lambda { |query|
where("first_name LIKE ? OR last_name LIKE ?", "%#{query}%", "%#{query}%")
}
# here i setup the scope for a foreign key
scope :school, -> school_id { where(:school_id => school_id) }
要了解有关此gem的更多信息,请参见文档链接。您必须按照侧边栏中的显示顺序进行所有操作,以更全面,更轻松地了解宝石