Tag search act_as_taggable_on rails

I created an application in which advertising banners can be saved in a database and marked with attributes, such as banner sizes (e.g. 300x250).

All this was done using acts_as_taggable_on .

Now what I want to do is search box, which when the user enters a size (for example, 300x250), then my page will filter to show only banner ads with the tags “300x250” (this does not need to be done with Ajax or whatever- anything else, the search button is ok)

I have currently installed a Ransack gem for search, but cannot figure out how to integrate it with acts_as_taggable_on .

Is there an easy way for me to have a search box in my view that will only search through tags and only refresh the page with tagged objects?

Thanks for any help you can offer!

+4
source share
1 answer

It is easy. We just have to write the right associations for ransack.

reflect_on_all_associations or reflections shows that the Banner has an association with the name :taggable , which leads to the ActsAsTaggableOn :: Tagging model, which belongs_to ActsAsTaggableOn :: Tag.

So then:

 @q = Banner.ransack(taggings_tag_name_cont: 'interesting') @banners = @q.result 

Of course, be free to use other matches such as eq , in , etc.

0
source

Source: https://habr.com/ru/post/1433873/


All Articles