我遇到了一個非常奇怪的問題,這是非常不一致的。我有一個單頁的應用程序,每隔幾秒鐘就會從我的Rails 3應用程序請求json。在服務器端,我使用active_record_store
來存儲瀏覽器的會話信息。Rails 3:會話被請求清除
問題是兩次請求之間有時會話信息被清除。這裏是一片日誌:
Started GET "/channels/2/show_next_slide.json?current_slide_slot_id=1806" for 127.0.0.1 at 2013-10-11 11:35:10 +0200
Processing by ChannelsController#show_next_slide as
Parameters: {"current_slide_slot_id"=>"1806", "id"=>"2"}
******* Test=
******* Session ID=ff7010bb1db040b90caa965b7736ad25
...
******* Test=1806
******* Session ID=ff7010bb1db040b90caa965b7736ad25
Completed 200 OK in 148ms (Views: 3.3ms | ActiveRecord: 4.3ms)
127.0.0.1 GET /channels/2/show_next_slide.json?current_slide_slot_id=1806 200 OK ChannelsController#show_next_slide JSON 148.1 (DB 4.3, View 3.3) {"current_slide_slot_id"=>"1806", "id"=>"2"} {}
Started GET "/assets/plugins/w/101d.png" for 127.0.0.1 at 2013-10-11 11:35:14 +0200
Served asset /plugins/w/101d.png - 304 Not Modified (0ms)
Started GET "/assets/plugins/w/102n.png" for 127.0.0.1 at 2013-10-11 11:35:14 +0200
Served asset /plugins/w/102n.png - 304 Not Modified (0ms)
Started GET "/assets/plugins/w/103n.png" for 127.0.0.1 at 2013-10-11 11:35:14 +0200
Served asset /plugins/w/103n.png - 304 Not Modified (0ms)
Started GET "/assets/plugins/w/109.png" for 127.0.0.1 at 2013-10-11 11:35:14 +0200
Served asset /plugins/w/109.png - 304 Not Modified (0ms)
Started GET "/channels/2/show_next_slide.json?current_slide_slot_id=1806" for 127.0.0.1 at 2013-10-11 11:35:15 +0200
Processing by ChannelsController#show_next_slide as
Parameters: {"current_slide_slot_id"=>"1806", "id"=>"2"}
******* Test=
******* Session ID=ff7010bb1db040b90caa965b7736ad25
在application_controller
我設置一個的before_filter和after_filter到session[:test]
內容寫入日誌(如圖******* Test=
)。正如您所看到的:第一個請求以session[:test]=1806
結尾,但隨後請求session[:test]
爲空。這偶爾會發生;不總是。
正如你所看到的,這兩個請求中的會話ID是相同的。此外,csrf
令牌傳遞得很好。只是爲了確保我禁用了protect_from_forgery
,但這沒有任何區別。所以這似乎不成問題。
我使用的唯一其他「第三方代碼」,並與會話做某事是設計。所以也許有什麼事情發生在那裏,但我想不出任何東西...
有沒有人有線索可能會發生在這裏?
附加信息
在application_controller,我調用以下方法在的before_filter和after_filter二者:
def log_session_for_debugging
logger.debug("******* Test=#{session[:test]}")
logger.debug("******* Session ID=#{request.session_options[:id]}")
end
我在show_next_slide控制器動作渲染命令之前權利一個值分配給session[:test]
:
respond_to do |format|
format.json {
session[:test] = @slide_slot.to_param
render :json => @slide_slot ? @slide_slot.as_json_with_slide_and_elements() : {}
}
end
設計會議會做些瘋狂的事情,但它通常創建一個新的會話,它不會與會話的內容模糊不清。我想你應該監視包含在rails響應中的請求頭。這可能表明存在問題。客戶端應始終在每個請求上接收會話數據。 – phoet
你能展示過濾器之前和之後嗎? –