Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorenadl/79931af20337e667982da95ee6119d92 to your computer and use it in GitHub Desktop.
Save lorenadl/79931af20337e667982da95ee6119d92 to your computer and use it in GitHub Desktop.
[Filterrific gem] How to filter elements in other actions with index parameters

[Filterrific gem] How to filter elements in other actions with index parameters

As stated in: http://filterrific.clearcove.ca/pages/action_controller_api.html, filterrific by default persists filters params:

   # * Options:
   #     * persistence_id: optional, defaults to "<controller>#<action>" string
   #       to isolate session persistence of multiple filterrific instances.
   #       Override this to share session persisted filter params between
   #       multiple filterrific instances. Set to `false` to disable session
   #       persistence.

So in actions other than index we could get the filtered records initializing filterrific with the session stored values:

participations_controller.rb

  def get_filtered_participations
   @filterrific = initialize_filterrific(
     @event.participations,
     session["participations#index"],
     select_options: {
       yes_no: [["Sì", true], ["No", false]],
       with_category_ids: Category.options_for_select,
       with_shipping_type_ids: ShippingType.options_for_select,
       with_event_ids: Event.options_for_select
     }
   ) or return
   @participations = @filterrific.find

 # Recover from invalid param sets, e.g., when a filter refers to the
 # database id of a record that doesn't exist any more.
 # In this case we reset filterrific and discard all filter params.
 rescue ActiveRecord::RecordNotFound => e
   # There is an issue with the persisted param_set. Reset it.
   puts "Had to reset filterrific params: #{e.message}"
   redirect_to(reset_filterrific_url(format: :html)) and return
 end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment