Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rails3-mongoid-devise

> 编程语言
Open source

An example Rails 3.2 app with Mongoid for data, Devise for authentication.

396 stars0 likes2 views
WebsiteGitHub

About

An example Rails 3.2 app with Mongoid for data, Devise for authentication.

h1. !http://railsapps.github.io/images/rails-36x36.jpg(Rails Application)! Rails 4.1

For a Rails 4.1 example application for Devise, see:

  • "rails-devise":https://github.com/RailsApps/rails-devise

A tutorial is available for Devise:

"Rails Devise Tutorial":http://railsapps.github.io/rails-devise/

h4. Similar Examples and Tutorials

This is one in a series of Rails example apps and tutorials from the "RailsApps Project":http://railsapps.github.io/. See a list of additional "Rails examples, tutorials, and starter apps":http://railsapps.github.io/rails-examples-tutorials.html. Related example applications may be useful:

  • "Learn Rails":https://github.com/RailsApps/learn-rails companion to the book "Learn Ruby on Rails":http://learn-rails.com/learn-ruby-on-rails.html
  • "Foundation and Rails":http://railsapps.github.io/rails-foundation/ shows how to integrate Foundation
  • "Bootstrap and Rails":http://railsapps.github.io/rails-bootstrap/ shows to integrate Bootstrap
  • "OmniAuth and Rails":https://github.com/RailsApps/rails-omniauth uses OmniAuth for authentication
  • "Devise and Rails":https://github.com/RailsApps/rails-devise uses Devise for authentication

h1. !http://railsapps.github.io/images/rails-36x36.jpg(Rails Application for Devise with RSpec and Cucumber)! Rails 3.2

Rails 3.2 example application shows how to use Devise with Mongoid.

  • "Devise":http://github.com/plataformatec/devise gives you ready-made authentication and user management.
  • MongoDB is used as a datastore with the "Mongoid":http://mongoid.org/ gem for quick development without schemas or migrations.

You really should use the "rails-devise":https://github.com/RailsApps/rails-devise newer version.

h2. What Is Implemented -- and What Is Not

This is a demonstration application that allows you to 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.

h2. Dependencies

Before generating your application, you will need:

  • The Ruby language (version 1.9.3 or 2.0.0)
  • The Rails gem (version 3.2.14)
  • A working installation of "MongoDB":http://www.mongodb.org/ (version 1.6.0 or newer)

See the article "Installing Rails":http://railsapps.github.io/installing-rails.html for advice about updating Rails and your development environment.

h4. 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":http://mxcl.github.com/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.

On the Debian GNU/Linux operating system, as of March 2013, the latest stable version is MongoDB 2.0.0. With MongoDB 2.0.0, the Mongoid gem must be version 3.0.x. See the "Mongoid installation instructions":http://mongoid.org/en/mongoid/docs/installation.html#installation. Change your Gemfile to use an earlier Mongoid version:

gem 'mongoid', '~> 3.0.1'

h2. Getting the Application

You have several options for getting the code.

h3. Fork

If you'd like to add features (or bug fixes) to improve the example application, you can fork the GitHub repo and "make pull requests":http://help.github.com/send-pull-requests/. Your code contributions are welcome!

h3. Clone

If you want to copy and customize the app with changes that are only useful for your own project, you can clone the GitHub repo. You'll need to search-and-replace the project name throughout the application. You probably should generate the app instead (see below). To clone:

$ git clone git://github.com/RailsApps/rails3-mongoid-devise.git

You'll need "git":http://git-scm.com/ on your machine. See "Rails and Git":http://railsapps.github.io/rails-git.html.

h3. Generate

If you want to use the project as a starter app, use the "Rails Composer":http://railsapps.github.io/rails-composer/ tool to generate a new version of the example app. You'll be able to give it your own project name when you generate the app. Generating the application gives you many additional options.

To build the example application, run the command:

$ rails new rails3-mongoid-devise -m https://raw.github.com/RailsApps/rails-composer/master/composer-Rails3_2.rb -T -O

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

The @$@ character indicates a shell prompt; don't include it when you run the command.

This creates a new Rails app named @rails3-mongoid-devise@ on your computer.

You'll see a prompt:

question Install an example application? 1) I want to build my own application 2) membership/subscription/saas 3) rails-prelaunch-signup 4) rails3-bootstrap-devise-cancan 5) rails3-devise-rspec-cucumber 6) rails3-mongoid-devise 7) rails3-mongoid-omniauth 8) rails3-subdomains

Choose rails3-mongoid-devise. The Rails Composer tool may give you other options (other choices may have been added since these notes were written).

The application generator template will ask you for additional preferences:

question Web server for development? 1) WEBrick (default) 2) Thin 3) Unicorn 4) Puma question Web server for production? 1) Same as development 2) Thin 3) Unicorn 4) Puma question Template engine? 1) ERB 2) Haml 3) Slim extras Set a robots.txt file to ban spiders? (y/n) extras Use or create a project-specific rvm gemset? (y/n) extras Create a GitHub repository? (y/n)

h4. Web Servers

Use the default WEBrick server for convenience. If you plan to deploy to Heroku, select "thin" as your production webserver.

h4. Template Engine

The example application uses the default "ERB" Rails template engine. Optionally, you can use another template engine, such as Haml or Slim. See instructions for "Haml and Rails":http://railsapps.github.io/rails-haml.html.

h4. Other Choices

Set a robots.txt file to ban spiders if you want to keep your new site out of Google search results.

It is a good idea to use "rvm":https://rvm.io/, the Ruby Version Manager, and create a project-specific rvm gemset (not available on Windows). See "Installing Rails":http://railsapps.github.io/installing-rails.html.

If you choose to create a GitHub repository, the generator will prompt you for a GitHub username and password.

h4. Troubleshooting

If you get an error "OpenSSL certificate verify failed" or "Gem::RemoteFetcher::FetchError: SSL_connect" see the article "OpenSSL errors and Rails":http://railsapps.github.io/openssl-certificate-verify-failed.html.

If you get an error like this:

Your bundle is complete! Use bundle show [gemname] to see where a bundled gem is installed. composer Running 'after bundler' callbacks. The template [...] could not be loaded. Error: You have already activated ..., but your Gemfile requires .... Using bundle exec may solve this.

It's due to conflicting gem versions. See the article "Rails Error: “You have already activated (…)”":http://railsapps.github.io/rails-error-you-have-already-activated.html.

Other problems? Check the "issues":https://github.com/RailsApps/rails3-mongoid-devise/issues.

h3. Edit the README

If you're storing the app in a GitHub repository, please edit the README files to add a description of the app and your contact info. If you don't change the README, people will think I am the author of your version of the application.

h2. Getting Started

h3. Install the Required Gems

Check the Gemfile to see which gems are used by this application.

If you used the "Rails Composer":http://railsapps.github.io/rails-composer/ tool to generate the example app, the application template script has already run the @bundle install@ command.

If not, you should run the @bundle install@ command to install the required gems on your computer:

$ bundle install

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

$ gem list

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.

I recommend using "rvm":https://rvm.io/, the Ruby Version Manager, to create a project-specific gemset for the application. See the article "Installing Rails":http://railsapps.github.io/installing-rails.html.

h3. 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":http://mongohub.todayclose.com/ app (for Mac OS X).

h3. Configure Email

This example application doesn't send email messages. However, if you want your application to send email messages (for example, if you plan to install the Devise @:confirmable@ module) you must configure the application for your email account. See the article "Send Email with Rails":http://railsapps.github.io/rails-send-email.html.

h3. Configure Devise

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

  • config/initializers/devise.rb

h3. Configuration File

The application uses the "figaro gem":https://github.com/laserlemon/figaro to set environment variables. Credentials for your administrator account and email account are set in the config/application.yml file. The .gitignore file prevents the config/application.yml file from being saved in the git repository so your credentials are kept private. See the article "Rails Environment Variables":http://railsapps.github.io/rails-environment-variables.html for more information.

Modify the file config/application.yml:

Add account credentials and API keys here.

See http://railsapps.github.io/rails-environment-variables.html

This file should be listed in .gitignore to keep your settings secret!

Each entry sets a local environment variable and overrides ENV variables in the Unix shell.

For example, setting:

GMAIL_USERNAME: Your_Gmail_Username

makes 'Your_Gmail_Username' available as ENV["GMAIL_USERNAME"]

Add application configuration variables here, as shown below.

GMAIL_USERNAME: Your_Username GMAIL_PASSWORD: Your_Password ADMIN_NAME: First User ADMIN_EMAIL: [email protected] ADMIN_PASSWORD: changeme

If you are planning to customize the application to send email using a Gmail account, you can add the user name and password needed for the application to send email. See the article "Send Email with Rails":http://railsapps.github.io/rails-send-email.html.

If you wish, set your name, email address, and password for the first user's account. If you prefer, you can use the default to sign in to the application and edit the account after deployment. It is always a good idea to change the password after the application is deployed.

All configuration values in the config/application.yml file are available anywhere in the application as environment variables. For example, @ENV["GMAIL_USERNAME"]@ will return the string "Your_Username".

If you prefer, you can delete the config/application.yml file and set each value as an environment variable in the Unix shell.

h3. Set Up a Database Seed File

The db/seeds.rb file initializes the database with default values. To keep some data private, and consolidate configuration settings in a single location, we use the config/application.yml file to set environment variables and then use the environment variables in the db/seeds.rb file.

puts 'DEFAULT USERS' user = User.

Issues· 13 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言