Improved Scaffolding for Ruby on Rails

Using the ActiveScaffold plugin to improve the default Ruby on Rails layouts.
Configuring Rails for PostgreSQL

I'm assuming you already have Ruby installed on your GNU/Linux system. If this is not the case, either install it from source from the Ruby Web site (see Resources) or install the Ruby package from your distribution's package manager (the ruby-full package on Ubuntu should include all you need). To install and use Rails, the RubyGems Package Manager needs to be installed into your Ruby environment. If RubyGems is not available within your distribution's package manager, pop on over to the RubyGems download page on RubyForge (see Resources), select the version of RubyGems that best matches your environment, and download the associated file. Installation is straightforward (note that the version you are working with may differ from that shown here):

tar zxvf rubygems-1.3.0.tgz
cd rubygems-1.3.0
sudo ruby setup.rb

If you are using Ubuntu (or one of its cousins), install the RubyGems package using apt:

sudo apt-get install rubygems

With RubyGems installed, you now can install Rails:

sudo gem install rails

Be sure to install all the suggested dependencies when prompted. This step takes a little while to complete, but it is a testament to the simplicity of Rails that you are ready to go once this command completes. One of the problems I've experienced with Perl-based WAFs is that installation can be a nightmare, especially when different versions of various CPAN modules throw up compatibility and dependency errors. Thankfully, there's no such maddeningly frustrating problems with Rails!

I did have one small problem with Rails on Ubuntu, which relates to the installation of the rails command in /usr/bin/, in that it wasn't there. Ubuntu expects you to install Rails using apt-get, but as I wanted the latest-and-greatest Rails, I went with the RubyGems installation method. To fix this small problem, create a link to the rails command, as follows:

sudo ln -s /var/lib/gems/1.8/bin/rails /usr/bin/rails

As we are using PostgreSQL as our database, we need to download and install the PostgreSQL Ruby gem. This, too, is straightforward:

sudo gem install postgres

If this causes an error, make sure the development libraries for Ruby are installed (called ruby1.8-dev on Ubuntu), as well as those for PostgreSQL (called libpq-dev). If compile-time errors still result (due to header files not being found, for instance), use this command instead (which should be entered on a single line):

POSTGRES_INCLUDE=/usr/include/postgresql   \
sudo gem install postgres

At this point, Ruby, PostgreSQL, the PostgreSQL gem and Rails are installed and ready for action.

Creating a New Rails Application

In a directory of your choosing, type the following command:

rails soccer_club --database=postgresql

This command creates a new Rails application called soccer_club, resulting in a long list of messages from Rails, and creates a new directory called soccer_club.

Preparing the Database Connection

Let's add some database tables to our application. Begin by first changing into the newly created soccer_club directory.

We could create the necessary tables using a series of SQL CREATE TABLE statements, patiently entering them into PostgreSQL's psql command-line tool. However, Rails provides a technology called Database Migrations that allows you to manipulate your database tables without directly using SQL. Migrations operate at a higher level, shielding the Web developer from the underlying SQL dialect. Before we create a Migration, let's tell our Rails application which database to use and provide a user name/password combination.

Edit the config/database.yml file associated with your Rails application, and change the development section to look like this (note that some default values have been suggested by Rails, but for our application, those values need to change):

  adapter: postgresql
  encoding: unicode
  database: soccer_development
  username: soccer_manager
  password: soccer_manager_password

On my Ubuntu system, PostgreSQL is configured to expect connections from a user name equal to the user ID of the currently logged-in user. This is called IDENT Authentication. What this means is that to access the soccer_development database with user ID soccer_manager, we need to be logged in to GNU/Linux as soccer_manager. That's not what we want (and it's not what Rails wants either), so we need to make a quick change to the bottom of the appropriate PostgreSQL configuration file (/etc/postgresql/8.3/main/pg_hba.conf), commenting out the ident sameuser line and adding a password line, as follows:

# "local" is for Unix domain socket connections only
local      all     all            password
# local    all     postgres       ident sameuser

After that edit, it's necessary to stop/start PostgreSQL to apply the change:

sudo /etc/init.d/postgresql-8.3 stop
sudo /etc/init.d/postgresql-8.3 start

To check that all is well with the Rails connection to the database, type the following within the top-level directory of your Rails application:

rake db:migrate

A single line of output results (in /home/barryp/rails/soccer_club on my system), which is Rails' way of telling us that everything is okay with the database connection. Any other message may indicate an error. If it is not immediately clear what the problem is (assuming, of course, that you have one), try appending --trace to the end of the above rake command.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Getting the article code to work with PostgreSQL.

barryp's picture

A reader, Ken Shaffer, emailed with an issue he had getting the example code (above) to work with his version of PostgreSQL. I initially suggested he try it with MySQL just to get things going but, to Ken's credit, he soldiered on and sorted out his problems. Here's a copy of Ken's e-mail to me letting me know what he did. Both Ken and I hope this information will be of use to other readers experiencing similar problems.

----------- start of Ken's email -----------
Hi Paul,
I did succeed in getting a compatible set of gems and activescaffold
for running the soccer club demo. Open source is so dynamic
that it's sometimes tricky getting compatible versions of things --
my hat's off to the people putting Linux distributions together.

For the Ubuntu 8.10 rails 2.1 (rake, rubygems) installed via the
Synaptic Package Manager, use the ruby_pg gem instead of the postgres
I understand the postgres gem has not been supported since 2006.
Also avoid the pg gem, which is not necessary. The activescaffold
install now needs to pick out the rails 2.1 (previous) version.
The following failed to get the rails 2.1 version (got the default 2.2
script/plugin install \
git:// \
-r rails-2.1
Found a code snippit (attached below) which worked.

Another (more painful) approach which worked is to update all the
rails 2.1.0 versions to the 2.2.2 versions (and also update rubygems).
The default activescaffold install will then work.

The below code snippit succeeded in getting the activescaffold version
compatible with rails 2.1

Comment 1 by mr.gaffo, Nov 18, 2008

---snip -----
Either (in vendor plugin):
git clone git://
[cd to active_scaffold ]
git checkout origin/rails-2.1
rm -rf .git
Or, pull down the newest rails-2.1 tarball from github.

Again, thanks for a great article. It was the combination of postgresql
and rails that caught my interest. Feel free to pass along any info, no
attribution needed.
----------- end of Ken's email -----------

Thanks for that, Ken!


Paul Barry