i am trying to make some Ajax calls to an controller which responds with JSON.
if session[:user]
render :json => "Some Data"
else
render :json => "You are not logged in"
end
The first time this action gets called by an authed user everything is ok and session[:user] is != nil.
The second time it gets called it is nil!
So it seems like rails is loosing it’s session as soon as i do render :json. I figured out that within the first call rails overrides the *_session-cookie with a new one. As consequence of that rails doesn’t know about the initial, authed, session.
If i don’t render the response as JSON everything works fine.
How to force rails to set the same sessionid in JSON rendered pages as in normal views?
After six days of searching I finally made it:
Seems like rails destroys the session because of the missing
X-CSRF-TokenHeader. I am adding this header now in in theajaxSendHook of JQuery:It’s working as expected now.