You can clone this app with git (see “Downloading the Example” below). Or you can generate a new Rails app using the app as a template (see “Generating the Application” below).
“Building It” Tutorial
A complete walkthrough tutorial is available on the GitHub wiki:
The tutorial documents each step I followed 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. Refer to the Rails Guides site for help if you are a beginner.
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.
What Is Implemented — and What Is Not
This is a barebones application that serves to demonstrate Mongoid and Devise working on Rails 3.
All the functionality of Devise is available (see a description of Devise for details). However, in this application, 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.
No testing (RSpec or otherwise) is implemented. Devise and Mongoid have their own test suites and this app only serves to demonstrate Mongoid and Devise working together on Rails 3.
This app is based on Rails 3 beta 4, Mongoid 2 beta 7, and Devise 1.1.rc2.
Before running this app, you need to install
- The Ruby language (version 1.8.7 or 1.9.2)
- Rails (version 3 beta 4 or newer)
- A working installation of MongoDB
I recommend installing rvm, the Ruby Version Manager, to manage multiple versions of Rails if you have a mix of Rails 3 and Rails 2 applications.
If you are using rvm, you can see a list of the Ruby versions currently installed:
$ rvm list
Check that appropriate versions of Ruby and Rails are installed in your development environment:
$ ruby -v
$ rails -v
If you intend to deploy to Heroku, note that Heroku supports Ruby version 1.8.7 but not Ruby version 1.9.2 (as of 18 June 2010).
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 the Mac OS X, the easiest way to install MongoDB is to install Homebrew and then use this script to install and set up MongoDB:
Generating the Application
To get started with a new Rails application based on this example, you can generate a new Rails app:
$ rails new app_name -m http://github.com/fortuity/rails3-mongoid-devise/raw/master/template.rb
This creates a new Rails app (with the
app_name you provide) on your computer. It includes everything in the example app (as described in the tutorial). Plus it offers you the option of setting up your view files using the Haml templating language.
If you wish to “change the recipe” to generate the app with your own customized options, you can copy and edit the file template.rb.
If you want to generate a new Rails application that uses Mongoid, Devise, Haml and jQuery you can use this template:
$ rails new app_name -m http://gist.github.com/raw/452364/gistfile1.txt
Downloading the Example
I recommend “Generating the Application” as described above. If that doesn’t work, or you simply wish to examine the example code, you can download the app (“clone the repository”) with the command
$ git clone git(at)github.com:fortuity/rails3-mongoid-devise.git
The source code is managed with Git (a version control system) and hosted at GitHub. You’ll need Git on your machine (install it from [http://git.or.cz/](http://git.or.cz/)).
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
Set Up Gems
About Required Gems
The application uses the following gems:
- rails (version 3.0.0.beta4)
- mongoid (version 2.0.0.beta7)
- bson_ext (version 1.0.1)
- devise (version 1.1.rc2)
If you wish to deploy to Heroku, you can include the heroku gem (version 1.9.10).
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.
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 Authentication
Set Up Configuration for Devise
This app uses Devise for user management and authentication. Devise is at http://github.com/plataformatec/devise.
You can modify the configuration file for Devise if you want to use something other than the defaults:
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:
puts 'SETTING UP DEFAULT USER LOGIN' user = User.create! :name => 'First User', :email => 'email@example.com', :password => 'please', :password_confirmation => 'please' puts 'New user created: ' << user.name
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://admin.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: firstname.lastname@example.org
- 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 http://heroku.com/ to set up an account.
Note that Heroku supports Ruby version 1.8.7 but not Ruby version 1.9.2 (as of 18 June 2010). If you’ve developed the app locally using Ruby version 1.9.2, you may wish to use Ruby Version Manager (rvm) to switch to Ruby version 1.8.7 and test the app before deploying to Heroku.
Make sure the Heroku gem is in your Gemfile. 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 http://docs.heroku.com/heroku-command for details.
Create Your Application on Heroku
Use the Heroku create command to create and name your new app> Specify the “bamboo-ree-1.8.7” stack so Heroku will allow you to use Rails 3.
$ heroku create _myapp_ --stack bamboo-ree-1.8.7
Heroku Add-on for MongoHQ
You can use a Heroku add-on to deploy your app using the MongoHQ service.
To enable the add-on, you can use the Heroku web interface or you can enter the following commands:
$ heroku addons:add mongohq:free
You can check that everything has been added correctly by running:
$ heroku info --app myapp
Be sure that the details show that the Heroku stack is “bamboo-ree-1.8.7”. If it is not, you can migrate to a new stack with the command
$ heroku stack:migrate bamboo-ree-1.8.7
Set Up Your Application on Heroku
Push your application to Heroku:
$ git push heroku master
Initialize your application database:
$ heroku rake db:seed
If you get the error message “failed to connect to any given host:port”, the config/application.rb file may not have the correct MongoHQ connection parameters.
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 application provides no useful functionality apart from demonstrating Devise and Mongoid working together on Rails 3.
The application does not include tests (RSpec or otherwise). It relies on Devise and Mongoid which include extensive tests. This application is intended to be a basis for your own customized application and (in most cases) you will be writing your own tests for your required behavior.
Documentation and Support
This application is provided without additional documentation or support.
If you make improvements to this application, please share with others.
- Fork the project on GitHub.
- Make your feature addition or bug fix.
- Commit with Git.
- Send the author 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.
For a simple Devise example using SQLite for Rails 2.3, see plataformatec/devise_example.
Daniel Kehoe (http://danielkehoe.com/) implemented the application and wrote the tutorial.
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.