The beta version of Ruby on Rails 3.0 was released back in February. It’s the result of the merging of the Rails project with Merb (another Ruby-based MVC framework), as well as a ton of bug fixes, performance enhancements, and new features.
I’ve been playing with it a bit for a personal project I’m working on, so I thought I’d share some of the cool new features with you. A lot of the posts out there about Rails 3 are aimed at hardcore Rails heads, so they’re unlikely to appeal to anyone who’s not used the framework before (or who’s only dabbled with it). I’m going to take a different approach here, and try and show you that now’s as good a time as any to jump into Rails and start playing around. If you tried Rails back in the 1.x or 2.x branches, maybe one of the new features I’ll be outlining will convince you to give it another try.
So, without further ado, let’s look at the first Rails 3 feature a new developer is likely to encounter. Rails 3 introduces Bundler, a very simple way of managing the dependencies of your application. Rails applications typically depend on a number of Ruby gems (what Ruby calls its libraries). For example, you might use Paperclip for handling models with attached files, Devise for authenticating users, or Cucumber for writing integration tests. Up until Rails 2.1, you basically had to manage any of your application’s dependencies yourself. If you deployed your application to a server that was missing a gem, or had a different version, it might just break without warning. Rails 2.1 (and 2.2) introduced some dependency management in the form of
gem.config, but its usage was a little arcane, and it was prone to breakage.
Rails 3′s Bundler makes gem management as simple as you can imagine. You put a file in your application’s root, called
Gemfile, where you specify which gems your application needs in the following format:
gem 'paperclip', '2.3.0'
The version string is optional; if it’s absent, Bundler will grab the latest version available on Gemcutter, the principal Ruby Gem repository. Once you’ve set up the Gemfile to your satisfaction, all you need to do is run this command from your application’s directory:
Bundler will then download all the required gems (and any dependencies they might have) to your machine.
These features alone make Bundler a great addition to Rails, but there’s more to it than that. Bundler also installs gems from GitHub seamlessly. All you need to do is specify a URL and a branch, and the rest is handled automatically when you run
bundle install. For instance, the current version of Paperclip has yet to be updated to support Rails 3. However, Jason King has forked the project on GitHub and patched it so that it works. So, to import the patched version into my app, all I need to write is:
gem 'paperclip', :git => 'git://github.com/JasonKing/paperclip.git', :branch => 'rails3'
My application will now grab that patched version. When the official Paperclip gem is updated, all I need to do is remove the
:branch options, and re-run
bundle install. I’ll now be running the version from Gemcutter.
Another great feature of Bundler is that you can specify the environments where a given gem is required by putting your gem statements into groups:
group :test do gem 'webrat' gem 'shoulda' end
With this code in my Gemfile, I can run the following on my production machine:
bundle install --without test
This is useful because I’d rather avoid worrying about managing the libraries required to compile Webrat and its dependencies on a machine on which they’ll never be used anyway.
Bundler is only one of the many new features in Rails 3; it just happens to be the first one you’ll encounter when setting up a new application. Next week I’ll be looking at a few features that more directly impact development. Until then, check out the release announcement for instructions on installing the beta, and the official release notes to find out about everything that’s in the new version.