简体   繁体   中英

Caching JSON actions in Rails with caches_action

I am having trouble caching a JSON action in my Rails controller. What happens is that, from an Android app, I make a request to the endpoint specifying the mime type "application/json" in the header but the response comes as "text/html" .

This is the only action I have that uses caches_action , and I am using caches_action so my filters aren't skipped (like in pages_action ).

I'm using:

caches_action :cards, expires_in: 1.day,
               cache_path: "mobile/cards/card_id_#{params[:card_id]}"

It works if I append .json to the request url, but I don't wanna do that, since all other JSON endpoints in this application do not require that.

Why does Rails force the response to be served as "text/html" ?

What could I be possibly doing wrong?

I fixed the problem just by adding format: :json to the route, as told by the official documentation :

The :format parameter is taken into account though. The safest way to cache by MIME type is to pass the format in the route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM