ransack--简单搜索

Ransack重写了MetaSearch,使用Ransack可以对模型创建简单的和高级两种搜索

下面是结合分页will_paginate的一个例子:

 

1.在gemfile中添加gem包, ransack,后执行bundle install

2.安装成功后,修改rails自动生成的脚手架

   在控制器中

 

 

  def index
    @search = Product.search(params[:q])
    @products = @search.result.paginate(:per_page => 5, :page => params[:page])
#   @products = Product.paginate(:per_page => 5, :page => params[:page])


    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end

 

   在视图中增加

 

   <%= search_form_for @search do |f| %>

  <%= f.label :name_cont, "Name contains" %>
  <%= f.text_field :name_cont %>
  <%= f.submit "Search" %>
<% end %>

 

 

  search_form_for生成搜索的表单,注意 :name_cont这个符号,它是又Product的name属性 和 _cont 组成的,意为搜索name中包含xx的product,查看html,搜索内容以 name="q[name_cont]" 传给控制器,控制器以params[:q]为参数,调用search方法,查找符合条件的products,最后,对@products进行分页,使用will_paginate,即对搜索结果再进行分页处理

 

 

在index页面,添加如下代码,进行分页:

 

 

<%= will_paginate @products %>
 

 

    刷新index页面

ransack--简单搜索_第1张图片

 

 二、多个搜索条件

 

     在视图页面,加上价格的筛选条件,直接修改index页面

 

 

<%= search_form_for @search do |f| %>
  <div class="field">
    <%= f.label :name_cont, "Name contains" %>
    <%= f.text_field :name_cont %>
  </div>
  <div class="field">
    <%= f.label :price_gteq, "Price Between" %>
    <%= f.text_field :price_gteq %>
    <%= f.label :price_lteq, "and " %>
    <%= f.text_field :price_lteq %>
  </div>
  <div class="actions"><%= f.submit "Search" %></div>
<% end %>

 

 

:price_gteq 和 :price_lteq 即价格大于等于(gteq = greater than euqal)并小于等于(lteq = less than euqal)

product的price

 

 

      eq: "equals"
      eq_any: "equals any"
      eq_all: "equals all"
      not_eq: "not equal to"
      not_eq_any: "not equal to any"
      not_eq_all: "not equal to all"
      matches: "matches"
      matches_any: "matches_any"
      matches_all: "matches all"
      does_not_match: "doesn't match"
      does_not_match_any: "doesn't match any"
      does_not_match_all: "doesn't match all"
      lt: "less than"
      lt_any: "less than any"
      lt_all: "less than all"
      lteq: "less than or equal to"
      lteq_any: "less than or equal to any"
      lteq_all: "less than or equal to all"
      gt: "greater than"
      gt_any: "greater than any"
      gt_all: "greater than all"
      gteq: "greater than or equal to"
      gteq_any: "greater than or equal to any"
      gteq_all: "greater than or equal to all"
      in: "in"
      in_any: "in any"
      in_all: "in all"
      not_in: "not in"
      not_in_any: "not in any"
      not_in_all: "not in all"
      cont: "contains"
      cont_any: "contains any"
      cont_all: "contains all"
      not_cont: "doesn't contain"
      not_cont_any: "doesn't contain any"
      not_cont_all: "doesn't contain all"
      start: "starts with"
      start_any: "starts with any"
      start_all: "starts with all"
      not_start: "doesn't start with"
      not_start_any: "doesn't start with any"
      not_start_all: "doesn't start with all"
      end: "ends with"
      end_any: "ends with any"
      end_all: "ends with all"
      not_end: "doesn't end with"
      not_end_any: "doesn't end with any"
      not_end_all: "doesn't end with all"
      'true': "is true"
      'false': "is false"
      present: "is present"
      blank: "is blank"
      'null': "is null"
      not_null: "is not null"
 

 

刷新index页面

 


ransack--简单搜索_第2张图片

 

三、对所搜结果进行排序

 

在index页面

 

 

    <tr>
      <th><%= sort_link @search,:name, "Name"  %></th>
      <th><%= sort_link @search,:price, "Price"  %></th>
      <th><%= sort_link @search,:area, "Area"  %></th>
    </tr>
 

 


ransack--简单搜索_第3张图片

 

官方文档:http://rubydoc.info/gems/ransack/0.7.0/frames

github:https://github.com/ernie/ransack

你可能感兴趣的:(search,will_paginate,ransack)