gem 'ransack'
使用:
-
params
使用:q
代替:search
- 使用
search_form_for
而不是form_for
- 需要调用
Ransack#result
获取搜索结果
controller:
def index
@q = Person.ransack(params[:q])
@people = @q.result(distinct: true)
end
view
<%= search_form_for @q do |f| %>
# Search if the name field contains...
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>
# Search if an associated articles.title starts with...
<%= f.label :articles_title_start %>
<%= f.search_field :articles_title_start %>
<%= f.submit %>
<% end %>
界面中的排序
<%= sort_link(@q, :显示的字段, 描述, default_order: :desc) %>