Rails 3.1 + Devise + RSpec + Cucumber
This is an example application for Rails 3.1.1 that shows how to use Devise with RSpec and Cucumber. Devise gives you ready-made authentication and user management. RSpec is a popular alternative to the Test::Unit testing framework. Cucumber is often used with RSpec for Behaviour Driven Development.
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.
Any issues? Please create a GitHub issue.
Follow the project on Twitter: @rails_apps. Please tweet some praise if you like what you’ve found.
Join the email list (low volume, announcements only) for project updates and my tips about Rails resources.
A complete walkthrough tutorial for Rails 3.1.1 is available on the GitHub wiki:
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. See a list of recommended books and online resources for learning Rails.
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 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.
Similar Examples and Tutorials
If you’d like to use the Mongoid ORM with the MongoDB datastore instead of ActiveRecord and a SQLite database, see the rails3-mongoid-devise example app and tutorial. Mongoid makes development quicker without schemas or migrations. The rails3-mongoid-devise example app and tutorial shows how to set up Devise and Mongoid with RSpec and Cucumber.
See an expanded list of more Rails examples, tutorials, and starter apps.
Before generating your application, you will need:
- The Ruby language (version 1.9.2)
$ ruby -v
- Rails 3.1.1
$ rails -v
- Rake 0.9.2
$ rake --version
You must update the standard Ruby installation from Rake 0.8.7 to Rake 0.9.2 before using the application templates to generate a new Rails app.
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://github.com/RailsApps/rails3-devise-rspec-cucumber.git
The source code is managed with Git (a version control system). You’ll need Git on your machine (install it from http://git-scm.com/).
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 Rails Application Templates repository.
To build the “rails3-devise-rspec-cucumber” example application, run the command:
$ rails new myapp -m https://github.com/RailsApps/rails3-application-templates/raw/master/rails3-devise-rspec-cucumber-template.rb -T
-T flag to skip Test::Unit files. Add the
-J flag to skip Prototype files for Rails 3.0 (not needed for Rails 3.1).
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 name
myapp) on your computer.
The application generator template will ask you for your preferences:
- Would you like to use jQuery?
- 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 Guard to automate your workflow?
- Would you like to use Devise for authentication?
- Would you like to install HTML5 Boilerplate?
- Normalize CSS and use Skeleton boilerplate?
- Normalize CSS for consistent styling across browsers?
- Completely reset all CSS to eliminate styling?
- Would you like to set a robots.txt file to ban spiders?
- Would you like to use ‘rails-footnotes’ during development?
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 Rails Apps Composer 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.
About Required Gems
The application uses the following gems:
See an example Rails 3.1.1 Gemfile.
See Managing Rails Versions and Gems for advice and details.
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.
Set Up Configuration for Devise
This app uses Devise for user management and authentication.
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:
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 database by running the command:
$ rake db:migrate
$ 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: firstname.lastname@example.org
- password: please
You should delete or change the pre-configured logins before you deploy your application.
Deploy to Heroku
For your convenience, here are instructions for deploying your app to Heroku. Heroku provides low cost, easily configured Rails application hosting.
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 with RSpec and Cucumber 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.
rake spec to run all RSpec tests.
rake cucumber (or more simply,
cucumber) to run all Cucumber scenarios and steps.
Documentation and Support
This is the only documentation.
For a Devise introduction, Ryan Bates offers a Railscast on Devise. You can find documentation for Devise at http://github.com/plataformatec/devise. There is an active Devise mailing list and you can submit Devise issues at GitHub.
Please create a GitHub issue if you identify any problems or have suggestions for improvements.
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 (http://danielkehoe.com/) implemented the application and wrote the tutorial.
Is the app useful to you? Follow the project on Twitter: @rails_apps
and tweet some praise. I’d love to know you were helped out by what I’ve put together.
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.