search by attribute_like_any using multiple words in one field [searchlogic]

被刻印的时光 ゝ 提交于 2019-12-13 04:25:46

问题


My form

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

Controller

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

This returns the correct result, but now my form shows the name as ["foo", "bar"] instead of what the user input ("foo bar").

What's the elegant way to handle this?

Appreciate any feedback


回答1:


Solution

Well, I found out the hard way first, then by asking another question, I inadvertently found a better answer to my original question. Here's the secondary question.

Model

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

Controller

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

Views

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

Stay tuned...

I'm having difficulty rallying up some of the more hard-to-answer questions for Searchlogic 2.x, but because tasks aren't always so straightforward, other questions tend to surface. Here's one I hope to answer that's not covered here.

How to sanitize form params for use with Searchlogic?



来源:https://stackoverflow.com/questions/3639818/search-by-attribute-like-any-using-multiple-words-in-one-field-searchlogic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!