在一个字段中使用多个单词按attribute_like_any搜索[searchlogic] [英] search by attribute_like_any using multiple words in one field [searchlogic]

查看:106
本文介绍了在一个字段中使用多个单词按attribute_like_any搜索[searchlogic]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格

<% 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").

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?

感谢任何反馈

推荐答案

解决方案

好吧,我首先发现了难题,然后又问了一个问题,无意中找到了我原来问题的更好答案.这是第二个问题.

# 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提出一些更难以回答的问题,但是由于任务并不总是那么简单,所以其他问题往往会浮出水面.我希望回答的是这里未涵盖的一个.


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.

如何清理与Searchlogic一起使用的表单参数?

这篇关于在一个字段中使用多个单词按attribute_like_any搜索[searchlogic]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆