Rails boilerplate project

After doing a couple of Rails projects I found myself doing the same setup for each project. On top of the template project I wanted user authentication support with mail activation. Whenever it was time to implement a new project I didn’t quite remember the exact process and ended up spending too much time searching for how-to-articles. This article is a summary of the steps required to create a project with support for user authentication based on restful_authentication.

To be able to implement this you will need to have a sufficiently new installation of Rails. I am using 2.1.1 but it may work on later or earlier versions. In addition, you will need to have support for git since the restful_authentication plugin has moved to a git repository.

The name “myproject” should be replaced with whatever project name you want to use.

Create project folder

$ rails myproject
$ cd myproject

Create databases

Based on where you plan to host the site, this should be done for all three databases: myproject_production, myproject_development and myproject_test. You may skip this step if you want to use the built-in sqlite3 support.
$ mysql -u root
>create database myproject_development character set utf8;

Update database settings in Rails (config/database.yml):

adapter: mysql
socket: /tmp/mysql.sock
database: myproject_development
username: rails
password: rails
encoding: utf8
timeout: 5000

Add similar lines for the other two databases. You must also setup an account in the MySQL server that has full permissions on the database(s).

Download the restful_authentication plugin

Change the name from using a dash to an underscore to avoid a potential error.
$ git clone git://github.com/technoweenie/restful-authentication.git vendor/plugins/restful_authentication

Install acts_as_state_machine

$ script/plugin install http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk/

Generate authentication scaffolding

Include –stateful flag to make restful_authentication use the acts_as_state_machine plugin.
$ script/generate authenticated user sessions --stateful

Add a route for the activation

In file: config/routes.rb
map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil

Add an observer

In file: config/environment.rb
config.active_record.observers = :user_observer

Create a home page controller

$ script/generate controller site index

Add a root route

In file: config/routes.rb
map.root :controller => "site", :view => "index"

Add visual notification

Create application.html.erb and add display code for notifications and error messages.
In file: app/views/layouts/application.html.erb
<%= flash[:notice] %>
<%= flash[:error] %>
<%= yield %>

Add webmaster email and site URL

In file: config/environment.rb
ADMIN_EMAIL = "postmaster@myproject.com"

In file: config/environments/development.rb
SITE_URL = "localhost:3000"

In file: config/environments/test.rb
SITE_URL = "localhost:3000"

In file: config/environments/production.rb
SITE_URL = "www.myproject.com"

Update template mailer code

In file: app/models/user_mailer.rb
Change “ADMINEMAIL” to “#{ADMIN_EMAIL}” (1 occurance). Change “YOURSITE” to “#{SITE_URL}” (3 occurances).

Add a mail initialiser by creating the file /config/initializers/mail.rb and add:
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
:location => '/usr/sbin/sendmail',
:arguments => '-i -t -f postmaster@myproject.com'

Additional items:

  • Add more fields to the user model (first and last name etc).
  • Include AuthenticatedSystem in application.rb instead of every controller.
  • Add before_filter :login_required to controllers that should require a logged in user.

Final steps

Finally, migrate the database and remove the default homepage
$ rake db:migrate
$ rm public/index.html

One thought on “Rails boilerplate project”

Comments are closed.