Improved Scaffolding for Ruby on Rails
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.
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.
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 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).
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).
Trending Topics
| Dia - The Diagram Creation Tool | Feb 13, 2012 |
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
- Fun with ethtool
- Linux--The Internet Appliance?
- Dia - The Diagram Creation Tool
- Readers' Choice Awards 2011
- Validate an E-Mail Address with PHP, the Right Way
- Stack Backtracing Inside Your Program
- Python for Android
- LiS: Linux STREAMS
- Building a Two-Node Linux Cluster with Heartbeat
- Three-Tier Architecture
- Dia
4 hours 29 min ago - Service units, is a daemon
6 hours 5 min ago - Tcp
6 hours 25 min ago - Lamenting more development of Dia
12 hours 17 min ago - multiboot that works well for me
22 hours 7 min ago - What's a good, AFFORDABLE aka
22 hours 8 min ago - Employment Posters
1 day 13 hours ago - Sure the best distro is
1 day 14 hours ago - BeOS was the best
1 day 17 hours ago - I use Wireshark on a daily
1 day 22 hours ago






Comments
Some of RoR examples
Some of RoR examples that i do
Ruby On Rails installation on Windows
Ruby on Rails installation in Ubuntu
autocompleter example in RoR
live Validation for RoR
Ismail Muhammad Noman
share-facts.blogspot.com
Getting the article code to work with PostgreSQL.
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