带有acts_as_taggable_on 的搜索表单(Rails 3) [英] Search form with acts_as_taggable_on (Rails 3)

查看:48
本文介绍了带有acts_as_taggable_on 的搜索表单(Rails 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索框来搜索产品.每个产品都有一个标题,并带有多个标签.

I have a searchbox to search for products. Each product has a title and is tagged with multiple tags.

我希望能够按标题或标签搜索产品.换句话说,如果我有一个名为绿茶"的产品和另一个标记为绿色、红色、蓝色"的产品,并且我在搜索框中输入绿色",我希望这两种产品都出现在搜索结果中.

I want to be able to search for products by title or tag. In other words if I have a product called "Green Tea" and another product tagged "green, red, blue" and I type "green" into the searchbox, I'd like both products to appear in the search results.

我使用的是 Rails 3、acts_as_taggable_on、Ruby 1.9.2.

I am using Rails 3, acts_as_taggable_on, Ruby 1.9.2.

推荐答案

在您的控制器中,显示搜索结果的操作可能如下所示(其中 :q 是您在搜索框中的查询字符串):

In your controller, the action that displays the search results could look something like this (where :q is your query string from the search box):

def results
  @products = Product.where("title LIKE ?", "%#{params[:q]}%") \
    | Product.tagged_with("%#{params[:q]}%")
end

这篇关于带有acts_as_taggable_on 的搜索表单(Rails 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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