About filters persistence, I'm using the following before_action
in ApplicationController
:
def get_query(cookie_key)
cookies.delete(cookie_key) if params[:clear]
cookies[cookie_key] = params[:q].to_json if params[:q]
@query = params[:q].presence || JSON.load(cookies[cookie_key])
end
Then, say for an Intervention
model, I have the following:
class InterventionsController < ApplicationController
before_action only: [:index] do
get_query('query_interventions')
end
def index
@q = Intervention.search(@query)
@interventions = @q.result
end
end
That way, if interventions_path
is called without the q
param, cookies['query_interventions']
is checked to access last persisted query. But when interventions_path
is called with the q
param, this new query is used and persisted for later use.
Also, if interventions_path
is called with the clear
param, the cookie is deleted.
Note this would raise a CookieOverflow
exception if more than 4k are stored, but this is between 1024 and 4096 UTF-8 characters and it is usually ok. If not, you should use other kind of session storage.