Search form with acts_as_taggable_on (Rails 3)

前端 未结 1 923
鱼传尺愫
鱼传尺愫 2021-01-27 03:37

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 othe

相关标签:
1条回答
  • 2021-01-27 04:17

    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 讨论(0)
提交回复
热议问题