Ever had a need to store params in some more reliable way than putting it in the Active record session? You should check the params_persistance plugin. Ruby on Rails params_persistance plugin allows you to store params keys into DB for further restoration.
Best thing is that params are automatically scoped to current user, so it is ideal for storing last filter params user used.
Installation is really simple. You just need to type in your console:
ruby script/plugin install git://github.com/stevo/params_persistance.git ruby script/generate saved_filter_params rake db:migrate
Though there is a gotcha here. In you Ruby on rails application controller there must be be current_user method available, returning user object or nil.
Then all you need to do is to add set_search_persistence in your controller. Example usage in you Ruby on Rails application:
(I am using here Inherited resources and searchlogic, but these are not needed for Ruby on Rails params_persistance usage)
class Invoicing::InvoicesController < InheritedResources::Base set_search_persistence #This will store params[:search] for current_user in db, and restore it (merge with params) whenever it is not explicitely passed protected def collection @search = Invoice.searchlogic(params[:search]) @invoices = @search.all.paginate :page => params[:page], :per_page => 20 end end set_search_persistence :name => "my_filter_params" #this will just change name, under which params are stored within DB set_search_persistence :params_key => :filter #this will store different params key (default is :search) set_search_persistence :actions => [:show, :index] #this will execute store/restore procedure on actions enumerated (default is :index)
If there is skip_filter_saving?(filter_name) method available for user instance, and it returns true, params for such a
user will not be stored/restored.
ex.
class User < ActiveRecord::Base devise :authenticatable, :recoverable, :rememberable, :validatable def skip_filter_saving?(filter) self.some_attribute_that_indicates_filter_should_be_skipped? end end
