【问题标题】:search by attribute_like_any using multiple words in one field [searchlogic]在一个字段中使用多个单词按 attribute_like_any 搜索 [searchlogic]
【发布时间】:2010-09-03 21:54:04
【问题描述】:

我的表格

<% form_for @search do |f| %>
  <%= f.input :name_like_any %>
  ...
<% end %>

控制器

@search = Product.search
@search.name_like_any(params[:search][:name_like_any].split(/\s+/))
@products = search.all

这会返回正确的结果,但现在我的表单将名称显示为 ["foo", "bar"] 而不是用户输入的内容 ("foo bar")。

处理这个问题的优雅方法是什么?

感谢任何反馈

【问题讨论】:

    标签: ruby-on-rails searchlogic


    【解决方案1】:

    解决方案

    好吧,我首先发现了困难的方法,然后通过问另一个问题,我无意中找到了对我原来问题的更好答案。这是secondary question

    型号

    # app/models/product.rb
    class Product < ActiveRecord::Base
      scope_procedure :keywords, lambda { |query|
        name_like_any(query.split(/\s+/))
      }
    end
    

    控制器

    # app/controllers/products_controller.rb
    class ProductsController < ApplicationController
      def index
        @search = Product.search(params[:search])
        @products = @search.all
      end
    end
    

    观看次数

    # app/views/products/index.html.erb
    <% form_for @search do |f| %>
      <%= f.label :keywords, "Quick Search" %>
      <%= f.input :keywords %>
      <%= f.submit, "Go" %>
    <% end %>
    

    敬请期待...

    我很难为 Searchlogic 2.x 找出一些更难回答的问题,但由于任务并不总是那么简单,其他问题往往会浮出水面。这是我希望回答的一个,这里没有涉及到。

    How to sanitize form params for use with Searchlogic?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-11
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      相关资源
      最近更新 更多