Rails 3.2 example application 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 used for Behaviour Driven Development.
Best of all, there’s a detailed tutorial to show how it’s built.
You can build this application in only a few minutes using the Rails Composer tool.
Follow the project on Twitter: @rails_apps. Please tweet some praise if you like what you’ve found.
A complete tutorial is available:
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 resources for Rails.
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. Devise provides user management so a visitor can register with an email address and password and create an account. Devise provides authentication so access to the site can be limited to users who are registered and logged in.
The rake db:seed
command sets up a database with two example users. The first user is designated as an administrator and can view a administrative page when logged in. The second user is restricted from accessing the administrative page.
This is one in a series of Rails example apps and tutorials from the RailsApps Project. See a list of additional Rails examples, tutorials, and starter apps.
This example application uses ActiveRecord and a SQLite database. You can use the Mongoid ORM with the MongoDB datastore instead, for faster development without schemas or migrations. The rails3-mongoid-devise example app and tutorial shows how to set up Devise and Mongoid with RSpec and Cucumber.
For an extended version of this example that adds CanCan for authorization (controlling access to administrative pages) and Twitter Bootstrap (for CSS styling) see the rails3-bootstrap-devise-cancan example application.
The rails-prelaunch-signup example and tutorial from the RailsApps project is also based on this example application.
Before generating your application, you will need:
- The Ruby language (version 1.9.3)
- Rails 3.2
See the article Installing Rails for advice about updating Rails and your development environment.
You have several options for getting the code.
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. Your code contributions are welcome!
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-devise-rspec-cucumber.git
You’ll need git on your machine. See Rails and Git.
If you want to use the project as a starter app, use the 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 myapp -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb -T
Use the -T
flag to skip Test::Unit files.
The $
character indicates a shell prompt; don’t include it when you run the command.
This creates a new Rails app (with the name “myapp”) on your computer. You can use a different name if you wish.
You’ll see a prompt:
question Install an example application? 1) I want to build my own application 2) rails3-bootstrap-devise-cancan 3) rails3-devise-rspec-cucumber 4) rails3-mongoid-devise 5) rails3-mongoid-omniauth 6) rails3-subdomains
Choose rails3-devise-rspec-cucumber. 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 Create a project-specific rvm gemset and .rvmrc? (y/n) extras Create a GitHub repository? (y/n)
Use the default WEBrick server for convenience. If you plan to deploy to Heroku, select “thin” as your production webserver.
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.
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, the Ruby Version Manager, and create a project-specific rvm gemset and .rvmrc file (not available on Windows). See Installing Rails.
If you choose to create a GitHub repository, the generator will prompt you for a GitHub username and password.
If you get an error “OpenSSL certificate verify failed” or “Gem::RemoteFetcher::FetchError: SSL_connect” see the article OpenSSL errors and Rails.
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 (…)”.
Other problems? Check the issues.
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.
Check the Gemfile to see which gems are used by this application.
If you used the 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, the Ruby Version Manager, to create a project-specific gemset for the application. See the article Installing Rails.
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.
You can modify the configuration file for Devise if you want to use something other than the defaults:
- config/initializers/devise.rb
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 => 'user@example.com', :password => 'please', :password_confirmation => 'please' puts 'New user created: ' << user.name user2 = User.create! :name => 'Second User', :email => 'user2@example.com', :password => 'please', :password_confirmation => 'please' puts 'New user created: ' << user2.name user.add_role :admin
Use the defaults or change the values for name, email, and password as you wish.
Prepare the database and add the default user to the database by running the commands:
$ rake db:migrate $ rake db:seed
Set the database for running tests:
$ rake db:test:prepare
If you’re not using rvm, the Ruby Version Manager, you should preface each rake command with bundle exec
. You don’t need to use bundle exec
if you are using rvm version 1.11.0 or newer.
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: user@example.com
- password: please
You should delete or change the pre-configured logins before you deploy your application.
If you test the app by starting the web server and then leave the server running while you install new gems, you’ll have to restart the server to see any changes. The same is true for changes to configuration files in the config folder. This can be confusing to new Rails developers because you can change files in the app folders without restarting the server. Stop the server each time after testing and you will avoid this issue.
For your convenience, here is a Tutorial for Rails on 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 example application contains a suite of RSpec unit tests and Cucumber scenarios and step definitions.
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.
Please send the author a message, create an issue, or submit a pull request if you can contribute improved RSpec or Cucumber files.
Problems? Check the issues.
Are you getting an error “OpenSSL certificate verify failed” when you try to generate a new Rails app from an application template? See suggestions to resolve the error Certificate Verify Failed.
The tutorial provides additional 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.
Your best source for help with problems is Stack Overflow. Your issue may have been encountered and addressed by others.
You can also try Rails Hotline, a free telephone hotline for Rails help staffed by volunteers.
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: @rails_apps
and tweet some praise. I’d love to know you were helped out by what I’ve put together.
Copyright © 2012 Daniel Kehoe