嗨,我正在使用filtering为我的用户页面添加搜索和过滤功能。
搜索工作正常,但当我尝试实现“按”筛选排序时,得到了undefined method[]‘for nil:NilClass`’error。
我在这里参考文件文档: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下面是错误的屏幕截图:

请帮帮忙。谢谢!
发布于 2017-01-10 18:17:23
同时在你的控制器中初始化。params不包含select_options。它们应包含select_options。现在它看起来是:
=>{“search_query”=>“”}
它应该类似于
=>{“search_query”=>“,”select_options“=>YOUR OPTIONS}
发布于 2018-10-24 23:55:52
在用户控制器中
您需要设置控制器以接受选择选项和其他需要的参数,就像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的信息,这里是指向文档的链接。您必须按照显示的顺序遵循侧栏中的所有步骤,以便更完整和更容易地理解宝石。
http://filterrific.clearcove.ca/
https://stackoverflow.com/questions/41517652
复制相似问题