Search code examples
ruby-on-railsfilterscoperansack

Application Wide Filter/Scope via Form in Rails


How can I implement and application wide scope/filter on a certain resource in Rails?

Example: My application can collect questions asked by certain people/teams for certain categories/sub-categories. The application has multiple pages with statistics on these questions (e.g. word analysis, questions by subcategory).

Now it would be nice to implement a "scope-like" condition on said statistics. Let's say I want the stats to only be for the questions asked within the last seven days. Then I switch to the next page with different stats and the scope stays the same.

Currently I am using the Ransack Gem for filtering. I would just add the filtering form on the main view (layout) so that one can modify the parameters. But how would the logic work in the backend? So that the filter params are passed back and forth.


Solution

  • Store the filtering params in the session like: sesssion[:filtering_params] = { whatever: they, are: here } and then they'll be available on subsequent requests.