Rails 3.1 is returning raw Javascript code when back button is used Rails 3.1 is returning raw Javascript code when back button is used javascript javascript

Rails 3.1 is returning raw Javascript code when back button is used


I'm very new to RoR so I'm not sure if my answer would be the best, but I ran into the same problem and managed to fix it. So here's my two cents.

It seems showing raw ajax response happens because of browser cache. I fixed the problem by adding the following header to prevent the response from begin cached.

response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"response.headers["Pragma"] = "no-cache"response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"


What Joon said above definitely worked. I just added an extra check to see if the request was from ajax before I added the headers. Thanks Joon!

if request.xhr?    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"    response.headers["Pragma"] = "no-cache"    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"end