1

I'm having a problem with sessions in rails 3.2 when I use mongoid as SessionStore. I decided to use Brian Hempel's fork of mongo_session_store gem, as it was updated last month.

Before switching to this gem, I was using the default cookie-based session store, and it was working without problem. But now, even flash doesn't work when I redirect to another page. Session data seems to be reset on every single request.

And as a side-question: every request creates a new session document in db! Is it right? I'm confused. =(

I put these lines in my Gemfile:

gem 'mongoid', :git => 'git://github.com/mongoid/mongoid.git'
gem 'bson_ext', "~> 1.5"
gem 'mongo_session_store-rails3', '~> 3.0.5'

Then in my 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

If any other part of my code is needed, please tell me to post it. Thanks in advance.

Aaron Gray
  • 11,283
  • 7
  • 55
  • 61
Abbas Mashayekh
  • 301
  • 3
  • 12

2 Answers2

4

Gem author here. Version 4.0.0 was just released, which supports Mongoid 3.

gem 'mongo_session_store-rails3', '~> 4.0.0'
Brian Hempel
  • 8,844
  • 2
  • 24
  • 19
1

Have similar and other issues in my project. After changed to branch of cubewebsites all works as expected.

Branch description:

This fork aims to address a couple of issues I was having with the master branch:

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

and

 <class:Session>': uninitialized constant ActionDispatch::Session::MongoidStore::Session::BSON (NameError)
Fernando Kosh
  • 3,426
  • 1
  • 34
  • 31