2

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!

swrobel
  • 4,053
  • 2
  • 33
  • 42

2 Answers2

2

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]}"}
Developer
  • 983
  • 12
  • 12
reillyse
  • 370
  • 4
  • 10
0

The main point of action caching is to prevent actions being run every time a new request is coming in. Unless defined explicitly params are not taken into consideration.

If you want to cache based on instances I would suggest doing Conditional Gets.

iltempo
  • 15,718
  • 8
  • 61
  • 72
  • My `params[:id]` is part of the route - something like `/object/:id` - so it gets cached separately since the path is different. – swrobel Mar 01 '12 at 18:48