I have a model that updates its results through ajax calls to and endpoint that I named /model/filter
, and I'm trying to incorporate search into the mix. I've added Ransack and it works just fine on the index (if I just search), but I want it to also work when I apply a filter.
In my controller function I have replaced @models = Model.scoped
with
def filter
@search = Model.search(params[:q])
@models = @search.result.scoped
# apply other filters...
end
However, when an ajax call executes this function, I get back a result that doesn't use the search terms.
I either want to ensure that params[:q]
reaches /model/filter
in the same state as it was in when /model/index
loaded (a JS solution), or I want to be able to access the same @search
object when I call filter
(a Ruby solution).
Am I way off base here, or is this something that's easy to do?
I found the solution after some more searching.
In /model/index
, I added Rails.cache.write("q",params[:q])
.
Then, in /model/filter
, I added params[:q] = Rails.cache.read("q")
before I called @search = Model.search(params[:q])