ruby-on-railsruby-on-rails-3ruby-on-rails-3.2action-caching

Set caches_action, :expires_in => dynamically


Every example of caches_action I've seen looks like:

caches_action, :expires_in => 5.minutes

but I'd like to set expires_in based on the expiration time of an object used in the action. Is there any way to reference that object when setting expires_in since the object in question is based on params sent to the action?

I tried using

caches_action, :expires_in => Object.find(params[:id])

but alas it won't let me reference params there. Suggestions welcome!


Solution

  • Looks like you need to use a Proc which can get passed the params of the controller. Here is an example

    caches_action :show, cache_path: Proc.new {|controller_instance| "some_cache_path_that_is_uniq/#{controller_instance.params[:id]}"}