Improved Scaffolding for Ruby on Rails

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

In the May 2007 issue of Linux Journal, I described my initial foray into the world of Ruby programming, combining Ruby with CGI and AJAX to produce a Web-based Ethernet Analyzer. Although I had fun putting that particular solution together, my real reason for getting to know Ruby was to allow me to work with Ruby on Rails, the highly regarded Web Application Framework (WAF).

I've looked at a number of WAFs available within the Perl and Python spaces. Way back in the March 2005 issue of Linux Journal, I described Maypole, one of Perl's first WAFs. Since then, I've explored Catalyst (a Maypole fork), Jifty and Gantry. Despite my extensive use of and acknowledged fondness for Perl, Rails had caught my eye, and it was an itch I just had to scratch.

Learn Ruby before Learning Rails

I have one piece of advice for anyone hoping to work with Rails in any meaningful way: get to know Ruby first. I initially had terrible trouble getting my head around what Rails was doing due to my inexperience with Ruby. When I changed my approach and set aside Rails in order to learn Ruby properly, my second pass at Rails made more sense. It was also more productive.

Is Rails Meant to Be This Ugly?

There is little doubt that Rails is a great WAF, worthy of all the praise continually heaped upon it. However, when you first start working with Rails, the default Web pages generated by the framework are anything but impressive. In fact, they are downright ugly, which can be a bit of a disappointment, especially if all you need is a quick Web application mockup. Granted, these default layouts are designed to be replaced by something nicer: professionally designed CSS Web pages. And, to be fair, the Rails folks do go to great lengths to stress this fact. However, if you are in a hurry, stopping to design some customer-friendly Web pages is a drag. What's needed is nice, modern CSS styling for the quick-and-dirty, in-a-hurry types like me. That's where ActiveScaffold comes in.

ActiveScaffold: a Rails Sweetener

ActiveScaffold is built on top of the standard Rails environment and is a plugin that, in the words of the project's Web site, “provides you with a wealth of dynamically created goodness”. What this goodness means to Rails developers is that ActiveScaffold provides a nice set of CSS pages and methods for interacting with your database tables. ActiveScaffold initially manages to do this, somewhat remarkably, with only a single, trivial code change to an existing Rails application.

In this article, I redevelop the Web-based soccer club database application that I created with Maypole back in 2005, this time using Rails with ActiveScaffold as the development platform. To add a slight twist to the proceedings, I use PostgreSQL as my database, as I've decided to give PostgreSQL a go having read Reuven Lerner's excellent series of articles comparing PostgreSQL to MySQL (see the April, May and June 2007 issues of LJ).

Preparing PostgreSQL

If you don't have PostgreSQL installed (and you are using Ubuntu or some other Debian-based distro), installation is straightforward:

sudo apt-get install postgresql

If your GNU/Linux distribution does not support apt, use your package manager to download and install PostgreSQL. With PostgreSQL running, become the postgres user on your system and create a new soccer_manager user:

sudo su - postgres
createuser -U postgres soccer_manager

Be sure to answer n (for no) to each of the questions posed by the createuser program, as the soccer_manager needs to be restricted to working solely within the soccer database (which we'll create in just a moment). Selecting n deliberately restricts the privileges awarded at this stage. Next, create a database, called soccer_development:

createdb -U postgres soccer_development

With the database and user created, enter the PostgreSQL interactive terminal (psql), and give the soccer_manager a password as well as user privileges to use the soccer_development database:

psql
postgres=# alter user soccer_manager with    
postgres-# password 'soccer_manager_password';
postgres=# grant all privileges on database   
postgres-# soccer_development to soccer_manager;
postgres=# \q

Note the use of the quit command, \q, which exits psql. At this point, we are done working directly with PostgreSQL. We could log in to psql as the soccer_manager user and start to create tables within the database using standard SQL, but we'll get Rails to handle these details for us (more on this in a little while).

______________________

Comments

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
gem.
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
ver):
script/plugin install \
git://github.com/activescaffold/active_scaffold.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

from http://code.google.com/p/activescaffold/issues/detail?id=626
Comment 1 by mr.gaffo, Nov 18, 2008

---snip -----
Either (in vendor plugin):
git clone git://github.com/activescaffold/active_scaffold.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.
--snip------

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.
Ken
----------- end of Ken's email -----------

Thanks for that, Ken!

Paul.

Paul Barry

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState