https://github.com/activerecord-hackery/ransack
添加Gemfile
gem 'ransack'
view中代码
<%= form_tag products_path, method: :get, class: "navbar-form navbar-left" do %><%= search_field_tag("q[name_cont]", params["q"] && params["q"]["name_cont"], class:"form-control input-sm search-form", placeholder: "输入商品名称")%><% end %>
controller里
def index @q = Product.ransack(params[:q]) @products = @q.result(distinct: true) end
其中 name_cont会对name字段进行模糊查询