This rails3-bootstrap-devise-cancan example application is outdated and has been replaced! For a Rails 4.1 example application for Devise with authorization, see rails-devise-pundit.
This Rails 3.2 example application shows how to use Devise with CanCan and Twitter Bootstrap.
- Devise gives you ready-made authentication and user management.
- CanCan provides authorization for administrator access.
- Twitter Bootstrap is a front-end framework for CSS styling.
This is a demonstration application that allows you to visit a home page and see a list of users. 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. CanCan is used for authorization, limiting access to only an administrator for designated pages. An administrative dashboard allows the administrator to delete any user or change a user’s role.
The rake db:seed
command sets up a database with an administrator who can view a administrative page when logged in. Additional users can be added; each is restricted from accessing the administrative page.
Before generating your application, you will need:
- The Ruby language (version 1.9.3 or 2.0.0)
- The Rails gem (version 3.2.13)
See the article Installing Rails for advice about updating Rails and your development environment.
You have several options for getting the code. You can fork, clone, or generate.
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 https://github.com/AlejandroFerroBejerano/rails_library.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 rails_library
This creates a new Rails app named @rails_library on your computer. You can use a different name if you wish.
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.
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.
See the article Installing Rails to make sure your development environment is prepared properly.
I recommend using rvm, the Ruby Version Manager, to create a project-specific gemset for the application. If you generate the application with the Rails Composer tool, you can create a project-specific gemset.
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
The application uses the figaro gem 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 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: admin@example.com ADMIN_PASSWORD: administrator ROLES: [admin, manager, reader]
Set the user name and password needed for the application to send email.
If you wish, set your name, email address, and password for an administrator’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 administrator’s password after the application is deployed.
Specify roles in the configuration file. You will need an “admin” role. Change the “user” and “VIP” roles as you wish.
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.
Prepare the database and add the default user to the database by running the commands:
$ rake db:migrate $ rake db:seed
Use rake db:reset
if you want to empty and reseed the database.
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 application 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.
If you are using the default values from the config/application.yml file, you can sign in as the administrator using:
- email: admin@example.com
- password: administrator
You’ll see a navigation link for Admin. Clicking the link will display a page with a list of users at
http://localhost:3000/users.
The second user will not see the Admin navigation link and will not be able to access the page at
http://localhost:3000/users.
Stop the server with Control-C.
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.
This application provides no useful functionality apart from demonstrating Devise with CanCan and Twitter Bootstrap working together on Rails 3. Add any models, controllers, and views that you need.
For more complex applications that use Devise, CanCan, and Twitter Bootstrap, see:
- rails-stripe-membership-saas example and tutorial
- rails-prelaunch-signup example and tutorial
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 RSpec tests.
Run rake cucumber
(or more simply, cucumber
) to run 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.
The tutorial provides additional documentation.
For a Devise introduction, Ryan Bates offers a Railscast on Devise. You can find documentation for Devise at https://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 first version of the aplication (only user model with devise).
Alejandro Ferro Bejerano and Miguel Ángel Garrido Astilleros adapted this project to the specification of the practise subject “Web System Development”
The main goal of this exercise is to develop a web application with Ruby on Rails in which we are going to implement a web system for managing a library.
Through the exercise solving is advisable to use software development good practices, like version control software (like git or mercurial), code hosting (like github or bitbucket) or TDD (Test Driven Development, using RSpec).
The students must handle the solution to the laboratory professor and show him the system
working.
We want to develop a Library Management Web Application in Ruby on Rails. This app should fulfil the following requirements:
- It should be accessible from any web browser
- Application should allow the user the management (CRUD operations) of:
- Books and publications
- Publications Borrowing and Circulation
- Readers and Users
- The app should have a User Management module, which permits users to access the system by authenticating themselves and determining user roles: - Administrator: Can manage users (admins, managers and readers), roles, books and loans.
- Manager: Can manage readers, books and loans.
- Reader: Can list its current and past loans, and also can manage a wish list.
- Publications: - Can be borrowed by only one reader if they are physical books
- Can be borrowed by several readers if they are in electronic form
- We should keep a history of loans
You are asked to develop a web app to solve the proposed problem.