2012-04-03 21 views
1

當我使用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 

如果任何其它部分我的代碼是需要的,請告訴我發佈它提前。

+0

如果會話文檔保存或不保存,您是否檢查數據庫? – shingara 2012-04-04 07:52:23

+0

是的。正如我所說的那樣,會話集合就在那裏,並且對於每個請求,都會在其中創建一個新的「會話」文檔。 – 2012-04-04 09:23:50

回答

4

寶石筆者在這裏。版本4.0.0剛剛發佈,它支持Mongoid 3.

gem 'mongo_session_store-rails3', '~> 4.0.0' 
1

在我的項目中有類似的問題和其他問題。 更改爲cubewebsites的分支後,所有工作均按預期工作。

科介紹:

這個分支旨在解決幾個我與主分支有問題:

<class:Session>': undefined method `collection_name=' for ActionDispatch::Session::MongoidStore::Session:Class (NoMethodError) 

<class:Session>': uninitialized constant ActionDispatch::Session::MongoidStore::Session::BSON (NameError)