Filterrific多过滤器会话持久化失效问题:已设置persistence_id=true但筛选条件仍重置
Hey there, let's break down why your Filterrific filter settings aren't sticking even after setting persistence_id: true—I've run into similar quirks with the gem before, so here are the key fixes to check:
1. Replace true with a Unique String for persistence_id
From Filterrific's core logic, persistence_id expects a unique string identifier (not a boolean) to namespace your filter's session data. Using true might create an ambiguous session key that the gem can't reliably read/write to.
Update your controller initialization to use a specific, unique string:
def filterrific_appointments initialize_filterrific( Appointment, params[:filterrific], select_options: { staff_member: User.staff.map { |p| [p.full_name, p.id] }, state: State.order(:name).pluck(:name, :id), with_account: [["Account Created", true], ["No Account", false]], }, default_filter_params: { staff_member: current_user.id, intake: true }, persistence_id: 'appointments_filter', # Use a unique string instead of true available_filters: %i[ staff_member state intake with_account ], sanitize_params: true, ) || return end
2. Validate Your Rails Session Configuration
Filterrific relies on Rails' session storage to persist filter data, so make sure your session setup is solid:
- Check
config/initializers/session_store.rb—the default:cookie_storeworks for most cases, but if your filter params are large (exceeding 4KB cookie limit), switch to a server-side store like:active_record_storeto avoid data truncation. - If using cache-based sessions (like
:mem_cache_store), confirm your cache service is running and configured correctly.
3. Debug Session Data to Confirm Storage
Add a quick debug line to your controller to verify if Filterrific is actually writing to the session:
def filterrific_appointments # Check if session contains your filter data puts "Filterrific session data: #{session[:filterrific_appointments_filter]}" initialize_filterrific( # ... your existing config ... ) || return end
If you don't see any data in the session after adjusting filters, that means the gem isn't persisting values at all—this usually points to a misconfigured persistence_id or session store issue.
4. Check for Default Params Overriding Persisted Data
Filterrific only applies default_filter_params if no persisted session data exists. But if your controller has additional logic that resets filter params after initialize_filterrific, it could overwrite the stored values. Double-check that no later code in the controller is modifying @filterrific or filter params.
5. Confirm Filterrific Version Compatibility
Make sure your Filterrific version is compatible with Rails 6.0.4.4. Older versions of the gem had bugs with session persistence in Rails 6. Run bundle update filterrific to get the latest compatible release, then retest.
内容的提问来源于stack exchange,提问作者AHaveles

