非常好用的模糊查询gem:ransack

https://github.com/activerecord-hackery/ransack

添加Gemfile

gem 'ransack'

view中代码

          <%= form_tag products_path, method: :get, class: "navbar-form navbar-left" do %>
            <div class="form-group">
              <%= search_field_tag("q[name_cont]", params["q"] && params["q"]["name_cont"],
                                 class:"form-control input-sm search-form", placeholder: "输入商品名称")%>
            </div>
          <% end %>


controller里

  def index
    @q = Product.ransack(params[:q])
    @products = @q.result(distinct: true)
  end 

其中 name_cont会对name字段进行模糊查询



你可能感兴趣的:(非常好用的模糊查询gem:ransack)