Search form with acts_as_taggable_on (Rails 3)

本秂侑毒 提交于 2019-12-02 10:27:15

问题


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.

I am using Rails 3, acts_as_taggable_on, Ruby 1.9.2.


回答1:


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


来源:https://stackoverflow.com/questions/5090682/search-form-with-acts-as-taggable-on-rails-3

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