當我使用mongoid作爲SessionStore時,我遇到了rails 3.2中的會話問題。我決定使用Brian Hempel's分叉mongo_session_store
寶石,因爲它已於上月更新。隨着mongo_session_store-rails3,會話似乎並不工作
在切換到這個gem之前,我使用了默認的基於cookie的會話存儲,並且沒有問題。但是現在,即使flash
在我重定向到另一個頁面時也不起作用。會話數據似乎在每個請求中都被重置。
作爲一個側面的問題:每個請求都會在db中創建一個新的會話文檔!這樣對嗎?我很困惑。 =(
我把我的Gemfile這些行:
gem 'mongoid', :git => 'git://github.com/mongoid/mongoid.git'
gem 'bson_ext', "~> 1.5"
gem 'mongo_session_store-rails3', '~> 3.0.5'
然後在我config/initializers/session_store.rb
:
SomeAppName::Application.config.session_store :mongoid_store
sessions_controller.rb:
class SessionsController < ApplicationController
def new
...
end
def create
user = User.find_by_identifier(params[:identifier])
if user && user.authenticate(params[:password])
session[:uid] = user._id
redirect_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render :new
end
end
def destroy
...
end
end
如果任何其它部分我的代碼是需要的,請告訴我發佈它提前。
如果會話文檔保存或不保存,您是否檢查數據庫? – shingara 2012-04-04 07:52:23
是的。正如我所說的那樣,會話集合就在那裏,並且對於每個請求,都會在其中創建一個新的「會話」文檔。 – 2012-04-04 09:23:50