在一个字段中使用多个单词按 attribute_like_any 进行搜索 [searchlogic]

发布于 2024-09-17 19:39:09 字数 423 浏览 2 评论 0原文

我的表单

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

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

感谢任何反馈

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2024-09-24 19:39:09

解决方案

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

模型

# 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 收集一些更难回答的问题,但由于任务并不总是那么简单,因此其他问题往往会浮现出来。我希望回答这里未涉及的问题。

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文