Rails RESTful Authentication Example Application

August 13, 2008

FOR RAILS 3.0: A newer and more current Rails authentication example app shows how to set up Rails with the Devise authentication gem. It’s got a detailed Rails authentication tutorial plus an application template that can be used to generate a starter app. It shows how to use RSpec and Cucumber for testing with Devise as well. There’s a similar example app that shows how to set up Devise for authentication with the Mongoid gem and MongoDB database for quick development without schemas or migrations.

UPDATE: Rails has advanced since this application was released in mid-2008. As of February 2010, Rails is currently at version 2.3.5 (and Rails 3 is in beta). Several Rails gems are now available that provide a turnkey authentication solution, including Authlogic, Devise, and Clearance. You can also look at Technoweenie’s restful-authentication generator. The Ruby Toolbox page for Rails Authentication shows which are most popular. I personally recommend Devise.

I’ve released an open source Rails application that provides an example of RESTful Authentication.

It provides a complete system for managing users, including sign up and verification of a new user’s email address, login with role-based access control, and a system of resetting forgotten passwords, all using a RESTful architecture.

You can easily customize the application for your own needs.

You can obtain the source code here:

It is based on recommendations from the forum discussion Restful Authentication With All the Bells and Whistles.


GitHub Tips: Removing a Remote Branch

April 28, 2008

If you are working on a branch and want to abandon it, what do you do?

It’s easy to remove it from your local repository:

$ git branch -d mybranch # delete branch "mybranch"

And then how do you remove it from a GitHub repository? This works:


$ git push git@github.com:<my_account>/<my_repository>.git :heads/<mybranch>

But there’s an easier way. Scott Chacon suggests:

You can also do (assuming ‘origin’ is the name of your remote):

git push origin :mybranch

Scott Chacon is the author of the Git Internals book from Peepcode.


Getting Started With Git and GitHub Hosting

April 13, 2008

The most popular revision control system for an open source Rails app is Git.

I installed Git using MacPorts with
sudo port install git-core.

After installing Git, I installed the Git bundle for TextMate.

I signed up for Git hosting at GitHub. The process is nearly self-explanatory but I consulted two tutorials, A Tutorial Introduction to Git and Setting up a new Rails app with Git. I discovered I needed to create an ssh public/private key pair on my local machine and add the public key to my GitHub profile before I could push the app to the GitHub repository.

I’ve found two screencasts about using Git wih Rails, from PeepCode and Using Git to Manage and Deploy your Rails Apps from Scott Chacon.

GitHub has their own Git Guides but the best instructions I’ve found for using Git are the Git Guides from the Insoshi open source project.


Setting Up Mac OS X 10.5 (Leopard) for Rails Development

February 17, 2008

Here are directions for setting up Mac OS X 10.5 (Leopard) for developing a Rails web application. This covers everything you need to set up a Macintosh from scratch for Rails development. These instructions are suitable for a developer working offsite or independently (if you’re in an office with a support staff you probably don’t need this!).

These instructions make a number of assumptions about your development environment and requirements. This is what I used in a recent typical project.

  1. If the Mac has been used before, and there’s reason to save what’s already on the machine, back up all the user folders you want to keep to an external drive.
  2. You’ll need the disks that came with the Mac or disks from a Mac OS X 10.5 upgrade package. Do an “Erase and Install” to perform a clean install of Mac OS 10.5 (Leopard).
    1. Once the Macintosh HD is selected, click “Options.”
    2. Select “Erase and Install.”
    3. For “Format disk as” select the default “Mac OS Extended (Journaled).”
    4. Click “OK” and complete the Mac OS X installation.
  3. Create a new account with the name “Computer Administrator” (or something suitable for an account you will only use for system administration) and the short name “admin.”
  4. Create any user accounts you’ll need for yourself or anyone else using the machine.
  5. From the admin account, install the applications you’ll need for development. Here’s what I use:
    1. TextMate – http://macromates.com/
    2. iTerminal – http://iterm.sourceforge.net/
    3. Navicat – http://www.navicat.com/
    4. Firefox – http://www.mozilla.com/en-US/firefox/
    5. Firebug – https://addons.mozilla.org/en-US/firefox/addon/1843
  6. I like to use Locomotive as a graphical interface to manage my Rails projects. Before Mac OS X 10.5 (Leopard) was released, Locomotive offered an easy way to install a Rails stack on a Mac. Now it’s no longer needed to run Rails but it is still useful as an interface to start and stop an app, open iTerminal or TextMate, and preview an app in a web browser. You might want to try it, though it is not needed. If you are using Locomotive, go to to the Locomotive web site and download and install the “/usr/local” Rails bundle.
  7. Our team uses Skype and Pyro for interteam communication.
    1. Pyro – http://www.karppinen.fi/pyro/
    2. Skype – http://www.skype.com/
  8. From the admin account, install the developer XCode Tools package:
    1. Double click the XcodeTools.mpkg package.
    2. On the “Standard install on ‘Mac HD'” screen, select “Customize”.
    3. Check “Mac OS X 10.3.9 Support”.
    4. Click “Install”.
  9. If you’ll be working with uploaded images (probably with attachment_fu), you’ll need to compile and install FreeImage at a later point in the process, in which case you’ll need some header files from an older version of Mac OS X. From the admin account, after installing the XCode Developer package, install the MacOSX10.3.9 developer tools package (this provides the old header files that you’ll need for compiling FreeImage later): Double click the MacOSX10.3.9.pkg file in the packages folder.
  10. Install the X Window System (X11) from the Leopard install disk when you install the XCode developer package:
    1. Install X11Documentation.pkg.
    2. Install X11SDK.pkg.
  11. You’ll need MacPorts to later install FreeImage and Git. Install MacPorts 1.5 to “/opt/local” using the package from macports.org:
    http://www.macports.org/install.php
  12. Set the “/etc/bashrc” and “/etc/profile” files for the admin user and everyone else.
    1. Set aliases in “/etc/bashrc”
    2. Set the PATH in “/etc/profile”
  13. Modify the PATH environment variable in the “/etc/profile” file to add a path to “/opt/local” for Macports.
  14. Modify the PATH environment variable in the “/etc/profile” file to add a path to “/usr/local/mysql/bin” for MySQL (which you’ll add next).
  15. Compile and install MySQL 5.0.45 using the shell script at hivelogic.com: http://hivelogic.com/articles/installing-mysql-on-mac-os-x/
  16. Check your MySQL version.
    1. In iTerminal, type “mysql –version”.
    2. You should see “mysql Ver 14.12 Distrib 5.0.45, for
      apple-darwin9.1.0 (i686)”.
  17. You may be using Subversion for revision control or you may need to download open source code that is only available via Subversion. Open iTerminal and make sure you have access to Subversion (“which svn”).

    1. In iTerminal, type “svn –version”
    2. You should see “svn, version 1.4.4 (r25188)” (or something similar).
  18. You may be using Git for revision control or you may need to download open source code that is only available via Git. Git does not come with Mac OS X 10.5 (Leopard) so you’ll need to install it using MacPorts: “sudo port install git-core”.
  19. Check your Ruby version.
    1. In iTerminal, type “ruby –version”.
    2. You should see “ruby 1.8.6 (2007-09-24 patchlevel 111)
      [universal-darwin9.0]”.
  20. See which Ruby gems are installed with “gem list –local”. If you know which gems you’ll need for your application, you can install them now.
  21. Update to the current version of Rails.
    1. In iTerminal, “sudo gem update rails”.
    2. In iTerminal, “rails –version” should display
      “Rails 2.0.2”.
  22. If you’ll be working with attachment_fu and uploaded images, install Freeimage and Imagescience.
    1. Install Freeimage with “sudo port install freeimage”.
    2. If you receive any errors, check the install of XCode Tools.
    3. This discussion may be helpful: http://www.ruby-forum.com/topic/129554
    4. Install Imagescience with “sudo gem install -y image_science”.
  23. Log in to your user account and add your applications to the dock for easy access. This is a good time to look in System Preferences and set System/Accounts “Login Items” for applications such as Mail that you want to launch as soon as you log in.
  24. Get Mail set up properly for your user account.
  25. In your user account, set up the web browsers Firefox and Safari (and any others you’ll use) in the dock. You may want to set the Firefox home page to “http://localhost:3000/&#8221; so you’ll see your local development version of the your app when you launch Firefox. It’s a good idea to develop in Firefox so you’ll be developing to a standards-based browser. You may want to set the Safari home page to your production web site so you can see your app after it is deployed with a non-Firefox browser. Even better, set up Microsoft Internet Explorer running under VMWare Fusion so you check the app for browser compatibility issues.
  26. Set up Navicat to connect to your MySQL databases.
    1. Connect to your development database on “localhost.”
    2. You may also use Navicat to connect to your staging or production databases on your hosting server. But unless you have the authority and can take responsibility for the consequences, be cautious about modifying data on the production database!
  27. In your user account, create a folder for your Rails projects.
  28. Open iTerminal, change directories to your Rails projects folder, and create a new Rails application. Or find an existing open source application at GitHub and do a “git clone” for an application you want to modify.
  29. Build your Rails app.