### Install Ransack Memory Gem Source: https://github.com/richardrails/ransack_memory/blob/master/README.md Add the Ransack Memory gem to your application's Gemfile to include its functionality. After adding the gem, run bundle install. ```ruby gem 'ransack_memory' ``` -------------------------------- ### Run Ransack Memory Generator Source: https://github.com/richardrails/ransack_memory/blob/master/README.md Execute the Ransack Memory generator to create necessary configuration files and initializers for the gem. ```shell rails generate ransack_memory ``` -------------------------------- ### Ransack Memory Configuration Source: https://github.com/richardrails/ransack_memory/blob/master/README.md Configure the Ransack Memory gem by modifying the generated initializer file. Key options include the Ransack parameter name and the format for session keys, allowing customization of how filters are stored and retrieved. ```ruby RansackMemory::Core.config = { param: :q, # this means the default Ransack param name for searching. You can change it session_key_format: '%controller_name%_%action_name%_%request_format%' # this means how the key used to store the information to the session will be stored. Currently it interpolates request parameters. You can customize it and use these vars to build a key that fits your needs } ``` -------------------------------- ### Kaminari Pagination Fix for Ransack Memory Source: https://github.com/richardrails/ransack_memory/blob/master/README.md If encountering issues with Kaminari pagination (e.g., inability to return to the first page) when using Ransack Memory, you can either configure Kaminari to keep parameters on the first page or update the 'first page' view partial. The view update involves merging parameters and potentially excluding filter-clearing parameters. ```ruby # Option 1: Kaminari configuration (in an initializer) # config.params_on_first_page = true # Option 2: Update Kaminari view partial # app/views/kaminari/_first_page.html.erb <%= link_to_unless current_page.first?, t('views.pagination.first').html_safe, url_for(params.merge({page: 1, cancel_filter: nil})), remote: remote, class: 'btn btn-secondary' %> ``` -------------------------------- ### Integrate Ransack Memory Concern in Controller Source: https://github.com/richardrails/ransack_memory/blob/master/README.md Include the RansackMemory::Concern in your base controller, typically ApplicationController. This enables the gem's core features for saving and loading filters. It's recommended to place the filter saving logic after authentication and before other controller actions. ```ruby class ApplicationController < ActionController::Base include RansackMemory::Concern # insert this line before_action :authenticate_user! # only if you use Devise gem before_action :save_and_load_filters # insert this line after Devise auth before filter (Devise gem is not necessary) end ``` -------------------------------- ### Load Saved Filters from Another Controller Action Source: https://github.com/richardrails/ransack_memory/blob/master/README.md To load filters saved from a different controller action into the current one, define a public method `set_session_key_identifier` in your controller. This method should return a string that identifies the session key to load from, allowing cross-action filter persistence. ```ruby def set_session_key_identifier 'projects_index_html' if action_name == 'my_another_action' end ``` -------------------------------- ### Clear Ransack Filters in Views Source: https://github.com/richardrails/ransack_memory/blob/master/README.md Use the `clear_filter` helper in your ERB views to provide a button or link that removes the saved Ransack filter parameters from the session. You can pass standard HTML attributes to customize its appearance and behavior. ```erb <%= clear_filter %> ``` ```erb <%= clear_filter title: 'Clear Filter', class: 'btn btn-primary', data: {confirm: 'Really?', my_data: 'something'} %> ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.