开发者

Search form with acts_as_taggable_on (Rails 3)

开发者 https://www.devze.com 2023-02-12 16:31 出处:网络
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

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, R开发者_StackOverflowuby 1.9.2.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号