rails/actionpack-action_caching

Workaround required when you want to cache JSON output that isn't explicitly params[:format] = "json"

bramski opened this issue · 5 comments

If you are simply producing JSON responses from your action, say, as an API call, the action_cache implicitly makes your response of content-type: "html" despite what your render may actually be doing. The workaround for this is to explicitly set the "format" to "json"

cimm commented

We had the same problem, workaround: we had our API routes default to JSON:

namespace :api, defaults: { format: :json } do
end

As @cimm said, use default format.
But on rail 4.1, NoMethodError - undefined method 'gsub' for :json:Symbol is occured
Use String.

namespace :api, defaults: { format: 'json' } do
end

It works to me

For would be Binger's in Rails 5.

get 'api/data', format: 'json'

No more weird text/html for your cached json responses.