Rails action caching and json responses

Viewed 2330

I am having problems responding with JSON to json request, while having action caching enabled.

I have a fairly simple controller which responds to both JSON and HTML and caching works fine for HTML response, but I noticed when I request JSON, and would normally respond with JSON, but with action cache enabled, it wraps the JSON output in the layout!

When I cleared the cache, it gives me an error that no default layout exists:

ArgumentError in ClipsController#index

There was no default layout for ClipsController in...

How do I return JSON for the cached action?

UPDATE, I suspect the error about layout has to do with the way I cache the actions:

caches_action :index, :layout => false

I'm not sure though, why it would still need a layout in the case of a JSON response, as without caching there is no problem.

temporarily I have modified the cache filter to not cache for JSON requests, but this is not an ideal work-around, what do I do if I do want to cache JSON?

caches_action :index, :layout => false, :if => Proc.new { |c| !c.request.format.json? }

2 Answers
Related