Rails session doesn't persist
I can't get Rails sessions to persist, both on a local environment and an external staging environment. Right now I'm just doing something basic, like session[:cart_id]=1
in one of my controllers. If I reload the page the session comes up empty {}
. I must be missing something basic because I can't find anything online about this being common.
UPDATE 1
Even the simplest code shows sessions not persisting, for example I can do something as simple as session[开发者_StackOverflow社区:counter] = session[:counter].to_i + 1
in my action, and then = debug session
in my view, and every time I refresh the browser I get:
{} {"session_id"=>"280031b7eb3b4bf612da85acc8815b3b", "counter"=>1}
and a new session_id
is generated each time.
UPDATE 2
I removed the :domain => :all
option from my session store and things look better, but still not persisting. Now the session_id
is generating only once, so that is persisting. But if I store an integer inside the session it still isn't persisting to the next request.
I had the same issue and it was driving me nuts.
It turned out that I had a protect_from_forgery
in my ApplicationController
which silently replaced the session with a NullSession
.
I noticed this when I ran raise session.class.inspect
.
Using protect_from_forgery with: :exception
instead reveals the underlying problem properly.
精彩评论