Rails 3:预测过滤(分面导航)

发布于 2024-10-14 03:40:02 字数 729 浏览 3 评论 0原文

用户当前可以通过设置复选框将过滤器应用于数据库查询,例如

Colour
[] Red
[] Green
[] Blue    

Shape
[] Round
[] Square

Size
[] Small
[] Medium
[] Large

单击复选框会触发 AJAX 请求,该请求将 where 条件附加到查询,例如

@mymodel = MyModel.scoped
@mymodel = @mymodel.where(:colour => params[:colour]) if params[:colour]
...

我现在想做的是添加计数在每个复选框之后指示如果选择给定的复选框将显示多少个结果。例如

Colour
[] Red (148)
[] Green (121)
[] Blue (136)
...

,单击复选框后,所有复选框旁边的计数都应刷新,并且不会显示与零结果相关的选项。这需要适用于所有过滤器类别,因此例如,如果数据库中没有任何小且红色的内容,则在选择“红色”时“小”选项应消失,并在取消选择时重新出现。

实现这种类型的预测过滤的最佳方法是什么?

非常感谢。

更新
更具体地说,我的问题是:在 Rails 中实现分面导航的最佳方法是什么?
(感谢@Pasta 确定了这个术语)

Users can currently apply filters to a database query by setting checkboxes, e.g.

Colour
[] Red
[] Green
[] Blue    

Shape
[] Round
[] Square

Size
[] Small
[] Medium
[] Large

Clicking on a checkbox triggers an AJAX request which appends the where condition to the query, e.g.

@mymodel = MyModel.scoped
@mymodel = @mymodel.where(:colour => params[:colour]) if params[:colour]
...

What I would like to do now is add a count after each checkbox to indicate how many results would be displayed if a given checkbox is selected. e.g.

Colour
[] Red (148)
[] Green (121)
[] Blue (136)
...

Upon clicking a checkbox, the counts next to all checkboxes should be refreshed, and options pertaining to a zero result will not be displayed. This needs to work across all filter categories, so for example, if there is nothing small and red in the database, the 'Small' option should disappear when 'Red' is selected, and re-appear when it is deselected.

What is the best way to achieve this type of predictive filtering?

Many thanks.

Update
More specifically, my question is: What is the best way to implement faceted navigation in Rails?
(Thanks @Pasta for the identifying the term for this)

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

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

发布评论

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

评论(4

合久必婚 2024-10-21 03:40:02

有相当多的开源搜索引擎可以为您提供分面搜索。我们使用 Thinking Sphinx 并且对此非常满意。看起来他们对 Rails 3 有初步支持,但可能值得一试。 facet 语法相当简单:

示例:

# This will return any search results for 'pancakes'
Article.search 'pancakes'

# This will return any facets for 'pancakes'
Article.facets 'pancakes'

您可能还想查看 SolrFerret——我相信这两个都支持分面搜索。

There are quite a few open-source search engines that will give you faceted search. We use Thinking Sphinx and have been very happy with it. It looks like they have preliminary support for Rails 3, but it might be worth a shot. The facet syntax is fairly straightforward:

Example:

# This will return any search results for 'pancakes'
Article.search 'pancakes'

# This will return any facets for 'pancakes'
Article.facets 'pancakes'

You might also want to look at Solr and Ferret -- both of which I believe support faceted search.

话少心凉 2024-10-21 03:40:02

查看分面搜索

干杯;

Take a look at faceted search.

Cheers;

叶落知秋 2024-10-21 03:40:02

尽管我无法完全回答,但我建议您考虑通过ajax更新此“表单”或部分内容。 Ryan 有一个 Railscast 43 会给你一些关于它如何工作的想法,但随后你可以研究如何使用 jquery 和 Rails3 来做所有奇特的事情。

although I can't answer fully, I suggest you look into updating this "form" or partial through ajax. Ryan had a railscast 43 will give you a few ideas on how it can work, but then you can research how to do all the fancy stuff with jquery and rails3.

李不 2024-10-21 03:40:02

我为基于 ActiveRecord 的过滤和构面计算编写了一个 gem,它不依赖于外部服务/deamons: https://github .com/fortytools/forty_facets

I wrote a gem for ActiveRecord based filtering and facet calculation, that does not depend on external services/deamons: https://github.com/fortytools/forty_facets

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