How force that session is loaded?

后端 未结 4 836
南旧
南旧 2020-12-03 10:47

I\'m working on an application that needs to use session id information. My session is stored in cookies. The problem I have is that my session is not immediately available

4条回答
  •  有刺的猬
    2020-12-03 11:14

    I found a way to force initialization of the session. Accessing the session apparently does not force initialization but writing into the session does. What I do in my controller is this now:

    class MyController < ApplicationController
      protect_from_forgery
      def index
        session["init"] = true
        do_stuff
      end
    end
    

    Still I'm not sure if this should be considered normal behavior in Rails. It doesn't look right to me having to write into the session to force initialization. Reading should be enough.

提交回复
热议问题