Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails action caching and json responses

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? }

like image 398
Victor S Avatar asked Aug 11 '12 22:08

Victor S


1 Answers

Seems like this might actually be a bug with action caching, but perhaps there's something I'm not understanding.

See this line to see where it goes wrong. Basically, if you've set the layout option to false, it doesn't care, and tries to render it with a layout anyway.

like image 121
heartpunk Avatar answered Nov 14 '22 13:11

heartpunk