Ruby on Rails

"Build Your Own Ruby On Rails Web Applications" review


This was 90% written in January 2007 but the draft was misplaced. Apologies to ORUG who organized obtained a copy for me in exchange for this review.

Ruby on Rails is a simply wonderful technology to develop web applications in. Like all technologies, especially ones with such depth and, at times, unique ways of working, it can take some time to get started using it to develop your own sites. Patrick Lenz comes to the rescue with his Build Your Own Ruby On Rails Web Applications book that I recommend for beginners.

"What's it all abou'?"

Over the course of the book's 400+ pages Mr Lenz gives a good foundation to build from, from installing all of the requisite tools, to basics of how to use the Ruby language that forms the basis of this system, to lots of good advice on testing your applications. The book builds up layer upon layer as it steps you through building a site similar to the community-driven link rating system Digg, all of which follow commonly used "best practices" (the techniques that help your sites be more stable, more flexible, etc).


The book reads very easily and does a good job of explaining the sample code provided as part of the Digg application.


An unfortunately common trait with many introductory books on technical topics is to hint at subjects related to the technology at hand, but then disregard it with a wave of the editor's delete key. This book was missing three key concepts that would have greatly rounded it off for beginners:

  • Sending email. This topic was waved away but would have been very valuable. When you think about it, the majority of web applications send email - either notifications, for verifying new accounts, or for custom newsletters, it is an extremely relevant topic and shouldn't have been skipped.
  • Interaction with 3rd party APIs. Lots of web applications use 3rd party APIs, whether it's verifying credit cards, submitting orders, obtaining mapping data, or getting the weather, there are a many, many good uses they can be put to (check if it's raining in your area, if so find stores that stock umbrellas and give you a map to them..). Interestingly this topic wasn't even mentioned, which was disappointing. Hopefully the next edition will have some good coverage.
  • REST. Everyone knows developers need more REST. Puns aside, Representational State Transfer is a new, and most would say improved, technique to pass data to and from web pages, APIs and services. Based off a more logical use of the HTTP system that binds the web together, REST defines that if you submit an ID in the URL you are reading a record, if you are submitting a form then you're either updating or creating a new record, etc, and so applications are structured to behave more cleanly in this regard. REST support was added to Rails v1.2, which this book is focused on, but it was a major oversight to not cover it. Again, a second edition should set the record straight.

Eth Ned

While not a perfect book, it does succeed with its intended goal, of easing you into writing your own Ruby on Rails applications. Some of the missing topics would have made the book more useful to intermediary users, but they shouldn't detract too much for beginners.

Recommended for beginners

Rails tip: restful_authentication without a username


I've personal never understood the point of having a username for web sites when 99% of them also require an email address that is unique to you - just use the email address! In the world of Rails development many developers use the excellent restful_authentication to provide the user login structure, but again out of the box it uses a username. Silly thing. So instead, here's how to make restful_authentication sit rather happy with just an email address.

The first issue is the create_users migration. By default a :login attribute is added which can be simply removed - yes, that's it. Next off is the User model. The first part of this is to remove all references to "login" from the validation statements, so e.g. instead of validates_presence_of :login, :email you have validates_presence_of :email

The second part is a little tricker: all methods that use :login have to be changed to use :email, e.g. the encrypt_password() method uses the login to make the security a little stronger, and, more importantly, the self.authentication() method is based off :login.

Once those changes have been done you can proceed to the next step. The next step is to update the views, both the users/new.rhtml and sessions/new.rhtml files need to tweaked. For users/new.rhtml just remove the paragraph that has the login field and you're done. For the sessions/new.rhtml file you need to replace all references of "login" to "email". The final part is to update the sessions controller, and once again just replace "login" with "email". That should do it. Now load up your app and enjoy simpler authentication!

Bonus Tip:

Now, make your user model even more useful and add first_name and last_name fields to the migration and app/views/user/new.rhtml file. One little thing to watch for - due to how restful_authentication works you'll want to also add those two fields to the attr_accessible line in your User.rb model file. Other than that, you'll make life much easier for both you and your users.

rubygems 1.2.0 causing problems for anyone else? (SOLVED!)


After updating to rubygems 1.2.0 I've noticed that my mongrels started returning the following colorful error on every XHR load, i.e. all AJAX requests fail but regular page loads are fine:

Mon Jun 23 23:39:15 -0400 2008: HTTP parse error, malformed request ( #<Mongrel::HttpParserError: Invalid HTTP format, parsing fails.>

Given that rubygems was the last thing I updated, I figured I'd try reverting to the previous 1.1.1 release using the following steps:

  • uninstall all versions of the rubygems-update gem that were installed:
    sudo gem uninstall rubygems-update
  • install the v1.1.1 rubygems-update:
    sudo gem install --version '1.1.1' rubygems-update
  • manually run the updater to ensure that the stable v1.1.1 release was active:
    sudo update_rubygems

A few minutes of churning away and... still no biscuit! ARGH!

More updates when I get this working.

UPDATE: This is a weird one. I was poking around working on an official bug report to the error when I noticed that my ~/.gemrc had a duplicate source entry for github. I removed the duplicate, restarted the mongrels and... it worked! Go figure.

mod_rails 2 RC is out, get it!


Anyone who does Ruby on Rails development should familiarize themselves with mod_rails aka Phusion Passenger as the brand new v2 RC is just amazing. Just take a look at those improvements: from buffered uploads so apps don't get stuck, to a tremendous reduction in the memory usage, Rack and WSGI support (yeah, it can run Python apps!), improved startup time... it's everything Rails needs to cross that last hurdle of acceptance with people who didn't like the deployment requirements for Mongrel et al. Awesome stuff!


Subscribe to Ruby on Rails