An example Rails 3 app with Devise for authentication and Mongoid for data. With a tutorial.
Pull request Compare This branch is 10 commits behind fortuity:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Rails3 + Mongoid + Devise

This example app combines Mongoid with Devise for a Rails 3 starter app. Mongoid is a datastore that gives you quick development without schemas or migrations. Devise gives you ready-made authentication and user management.

Best of all, there’s a detailed tutorial (walk-through) to show how it’s built.

You can clone this app or generate a new Rails application using this app as a template.

Follow on Twitter

To keep up to date with development of this app, follow the project on Twitter:

Any issues? Please create an Issue on GitHub.

“Building It” Tutorial

A complete walkthrough tutorial is available on the GitHub wiki:

View the Tutorial

The tutorial documents each step to follow to create the application. Every step is documented concisely, so a complete beginner can create this application without any additional knowledge. However, no explanation is offered for any of the steps, so if you are a beginner, you’re advised to look for an introduction to Rails elsewhere.

If you simply wish to modify the application for your own project, you can download the application and set it up as described below, without following the tutorial.

Similar Applications

Plataformatec’s Simple Devise Example

Simple Devise example using SQLite for Rails 3.

Rails3 + Devise + RSpec + Cucumber

Daniel Kehoe’s example Rails 3 app with Devise + RSpec + Cucumber. With a detailed tutorial.

Fernando Tapia Rico’s rails3-mongoid-devise-omniauth with Tutorial

Extended with OmniAuth (for authentication via OpenID, Facebook, OAuth, etc.)

Andi Altendorfer’s CBA

Extended with OmniAuth (for authentication via OpenID, Facebook, OAuth, etc.), CanCan (adding authorization by roles), and Paperclip (for avatars).

Kristian Mandrup’s Cream

Integrates Devise, Roles, and CanCan with Permits. For Mongoid or other ORMs.

What Is Implemented — and What Is Not

This is a barebones application that serves to demonstrate Mongoid and Devise working on Rails 3.

All you can do is visit a home page and see a list of users. With the default user’s email and password (supplied below), you can log in and view details for each user. You can customize this app as you need.


Before running this app, you need to install

  • The Ruby language (version 1.8.7 or 1.9.2)
  • Rails (version 3.0.7)
  • A working installation of MongoDB (version 1.6.0 or newer)

I recommend installing rvm, the Ruby Version Manager, to manage multiple versions of Rails.

Check that appropriate versions of Ruby and Rails are installed in your development environment:
$ ruby -v
$ rails -v

Installing MongoDB

If you don’t have MongoDB installed on your computer, you’ll need to install it and set it up to be always running on your computer (run at launch). On Mac OS X, the easiest way to install MongoDB is to install Homebrew and then run the following:

brew install mongodb

Homebrew will provide post-installation instructions to get MongoDB running. The last line of the installation output shows you the MongoDB install location (for example, /usr/local/Cellar/mongodb/1.8.0-x86_64). You’ll find the MongoDB configuration file there. After an installation using Homebrew, the default data directory will be /usr/local/var/mongodb.

Getting the Application

You have several options for getting the code.

Downloading the Code

If you simply wish to examine the example code, you can download the code (“clone the repository”) with the command

$ git clone git://

The source code is managed with Git (a version control system). You’ll need Git on your machine (install it from

Using the Ready-Made Application Template

You can use an application template to generate a new version of the example app. You’ll find an application template for this app in the fortuity/rails3-application-templates repository.

Use the command:

$ rails new APP_NAME -m -T -O -J

Use the -T -O -J flags to skip Test::Unit files, Active Record files, and Prototype files.

You MUST be using Rails 3.0.4 or newer. Generating a Rails application from an “HTTPS” URL does not work in Rails 3.0.3 and earlier versions.

This creates a new Rails app (with the APP_NAME you provide) on your computer.

The application generator template will ask you for your preferences:

  • Would you like to use jQuery instead of Prototype?
  • Would you like to use jQuery UI?
  • Would you like to use Haml instead of ERB?
  • Would you like to use RSpec instead of TestUnit?
  • Would you like to use factory_girl for test fixtures with RSpec?
  • Would you like to use Cucumber for your BDD?
  • Would you like to use Mongoid to connect to a MongoDB database?
  • Would you like to use Devise for authentication?
  • Would you like to set a robots.txt file to ban spiders?

Use “Recipes” to Customize an Application Template

The tutorial shows how a customized application template can be assembled from “recipes.” The application template was created using the rails3_devise_wizard gem which provides a convenient way to assemble a reusable application template by selecting various “recipes” for popular Rails development packages.

Please Remember: Edit the README

If you’re open sourcing the app on GitHub, please edit the README file to add a description of the app and your contact info. Changing the README is important if you’re using a clone of the example app. I’ve been mistaken (and contacted) as the author of apps that are copied from my example.

Getting Started

About Required Gems

The application uses the following gems. I recommend checking for newer versions of these gems before proceeding:

If you are able to build the app with a newer gem, please create an issue on GitHub and I will update the app.

Install the Required Gems

Install the required gems on your computer:

$ bundle install

You can check which gems are installed on your computer with:

$ gem list --local

Keep in mind that you have installed these gems locally. When you deploy the app to another server, the same gems (and versions) must be available.

Configure Mongoid

Mongoid provides access to the MongoDB database from Rails.

You can use the default configuration found in the file config/mongoid.yml.

If you want to see what’s in your MongoDB databases, I recommend using the MongoHub app (for Mac OS X).

Set Up Configuration for Devise

This app uses Devise for user management and authentication. Devise is at

You can modify the configuration file for Devise if you want to use something other than the defaults:


Configure Email for Devise

Configure email by modifying


and setting the return email address for emails sent from the application.

You may need to set values for your mailhost in


Create a Default User

Set Up a Database Seed File

You’ll want to set up a default user so you can easily log in to test the app. You can modify the file db/seeds.rb for your own name, email and password:

Mongoid.master.collections.reject { |c| =~ /^system/}.each(&:drop)
user = User.create! :name => 'First User', :email => '', :password => 'please', :password_confirmation => 'please'
puts 'New user created: ' <<

Use the defaults or change the values for name, email, and password as you wish.

Seed the Database

Add the default user to the MongoDB database by running the command:

$ rake db:seed

Test the App

You can check that your app runs properly by entering the command

$ rails server

To see your application in action, open a browser window and navigate to http://localhost:3000/. You should see the default user listed on the home page. When you click on the user’s name, you should be required to log in before seeing the user’s detail page.

To sign in as the default user, (unless you’ve changed it) use

  • email:
  • password: please

You should delete or change the pre-configured logins before you deploy your application.

Deploying to Heroku

Set Up Heroku

For your convenience, here are instructions for deploying your app to Heroku. Heroku provides low cost, easily configured Rails application hosting.

To deploy this app to Heroku, you must have a Heroku account. If you need to obtain one, visit to set up an account.

Make sure the Heroku gem is in your Gemfile (check for the latest heroku gem):

@gem “heroku”

If it’s not, add it and run

$ bundle install

to set up your gems again.

Add your public key immediately after installing the heroku gem so that you can use git to push or clone Heroku app repositories. See for details.

Create Your Application on Heroku

Use the Heroku create command to create and name your new app.

$ heroku create _myapp_

As of 9 February 2011, bamboo-ree-1.8.7 is the default stack for new Heroku apps.

If you want to use Ruby 1.9.2, you can create your app with:

heroku create _myapp_ --stack bamboo-mri-1.9.2

Heroku Add-on for MongoHQ

You can use a Heroku add-on to deploy your app using the MongoHQ service. See details about the service and details about installation.

To enable the add-on, you can use the Heroku web interface or you can enter the following commands:

$ heroku addons:add mongohq:free

Check Heroku Configuration

You can check that everything has been added correctly by running:

$ heroku info --app myapp

If you see Stack: bamboo-ree-1.8.7 and you want to use Ruby 1.9.2, you can migrate:

heroku stack:migrate bamboo-mri-1.9.2

Push Your Application to Heroku

Push your application to Heroku:

$ git push heroku master

Initialize your application database:

$ heroku rake db:seed

Fix Problems Connecting to MongoHQ

If you get the error message “failed to connect to any given host:port” or “Failed to connect to a master node at localhost:27017”, the config/mongoid.yml file may not have the correct MongoHQ connection parameters.

If the file config/mongoid.yml contains this:

# set these environment variables on your prod server
  host: <%= ENV['MONGOID_HOST'] %>
  port: <%= ENV['MONGOID_PORT'] %>
  username: <%= ENV['MONGOID_USERNAME'] %>
  password: <%= ENV['MONGOID_PASSWORD'] %>
  database: <%= ENV['MONGOID_DATABASE'] %>

modify it to look like this:

# set these environment variables on your prod server
  uri: <%= ENV['MONGOHQ_URL'] %>

Then push your application to Heroku again with $ git push heroku master and run $ heroku rake db:seed again.

Visit Your Site

Open your Heroku site in your default web browser:

$ heroku open


If you get errors, you can troubleshoot by reviewing the log files:

$ heroku logs


Devise provides a variety of features for implementing authentication. See the Devise documentation for options.

This example application and tutorial demonstrates Devise and Mongoid working together on Rails 3. Add any models, controllers, and views that you need.


The application contains RSpec unit tests and Cucumber scenarios and steps. The tests are minimal and can be improved.

Please send the author a message, create an issue, or submit a pull request if you can contribute improved RSpec or Cucumber files.

After installing the application, run rake -T to check that rake tasks for RSpec and Cucumber are available.

Run rake spec to run all RSpec tests.

Run rake cucumber (or more simply, cucumber) to run all Cucumber scenarios and steps.

You can browse and search the Cucumber features at the Relish website.

Documentation and Support

See the Tutorial for this app for details of how it was built. Please create an Issue on GitHub if you identify any problems or have suggestions for improvements.

For a Mongoid introduction, Ryan Bates offers a Railscast on Mongoid. You can find documentation for Mongoid at There is an active Mongoid mailing list and you can submit Mongoid issues at GitHub.

For a Devise introduction, Ryan Bates offers a Railscast on Devise. You can find documentation for Devise at There is an active Devise mailing list and you can submit Devise issues at GitHub.

This application is provided without additional documentation or support.


If you make improvements to this application, please share with others.

Send the author a message, create an issue, or fork the project and submit a pull request.

If you add functionality to this application, create an alternative implementation, or build an application that is similar, please contact me and I’ll add a note to the README so that others can find your work.


Daniel Kehoe ( implemented the application and wrote the tutorial.

Is the app useful to you? Follow the project on Twitter:
and tweet some praise. I’d love to know you were helped out by what I’ve put together.

Any issues? Please create an Issue on GitHub.


Thank you to contributor Nakort Valles for updating to newer gems.

Thank you for improvements to the tutorial by contributors Cory Foy, Luca G. Soave, Bob Clewell, and Justin Workman.


Public Domain Dedication

This work is a compilation and derivation from other previously released works. With the exception of various included works, which may be restricted by other licenses, the author or authors of this code dedicate any and all copyright interest in this code to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this code under copyright law.