Rails 2: The Top Five Features List
It's the start of the new year, and as usual the media is trotting out their annual "lists" for 2007 ad nauseum. Best sports moments, worst political gaffes, sexiest celebrities... you name the topic and somebody's come up with a list itemizing its relevance in 2007. I thought I'd pen my first Developer.com column of the year in similar fashion, highlighting my personal take on top five features of the new Rails 2 release. Keep in mind this isn't a comprehensive breakdown, nor is it a summary of the most complicated new features, but rather it's an overview of a few key new features which are already making me a more productive developer. And that's ultimately the point of Rails, isn't it?
Feature #1: Even Easier View Management
In an effort to stay DRY (Don't Repeat Yourself), Rails developers often use partials to maintain a single data format that can be invoked from within numerous controller methods. For instance, consider the following controller, which might be used to retrieve URLs from a database:
class LinksController < ApplicationController
def index
@links = Link.find(:all)
end
def show
@link = Link.find(params[:id])
end
...
end
A single partial would be used for both the index and show methods. It might look like this:
<p><%= link_to h(link.name), link.url %></p>
Finally, the respective views would look like this:
# index view <%= render :partial => 'link', :collection => @links %> # show view <h3><%= @link.name %></h3> <%= render :partial => 'link', :object => @link %>
Rails 2 allows you to create even more succinct view code when referencing partials. Running Rails 2, the following views can be rewritten like so:
# index view <%= render :partial => @links %> # show view <h3><%= @link.name %></h3> <%= render :partial => @link %>
Feature #2: Very Easy Authentication
I've used the Restful Authentication plugin on several Rails projects, and find it to be an incredibly intuitive and easy way to manage all aspects of user authentication, including registering, logging in to, and out of the application. But perhaps it's overkill if you're simply looking for an easy way to password protect a page. Rails 2 resolves this dilemma by introducing a basic HTTP authentication feature. To use it, you create a before_filter within the controller you'd like to protect, and within the filter method use authenticate_or_request_with_http_basic, like so:
class AdminController < ApplicationController
before_filter :authenticate
protected
def authenticate
authenticate_or_request_with_http_basic do |username,
password| username == 'jason' && password == 'supersecret'
end
...
end
When the user attempts to access any resource found under the Admin controller, he'll be greeted with the familiar authentication prompt. Entering a username of jason and a password of supersecret will grant him access; otherwise, he'll be prompted anew.
Of course, hardcoding a username and password into the method isn't exactly practical. Instead, you can invoke a method that will check against the database for a matching login pair.
Feature #3: Condensed Migrations Syntax
Migrations is far-and-away one of my favorite features of the Rails framework. In fact, I've written extensively about the topic in recent articles published here on Developer.com (See "Raking MySQL Over Rails" and "Manage Your MySQL Database with Rails' Migrations"). Indeed, the ability to manage your database schemas using a distinctly programmatic approach is truly compelling to developers like myself who prefer to keep database-related tasks at arms' length.
Evolving a schema programmatically is indeed appealing, but even Migrations syntax can become a bit tedious over the course of an application's lifetime. For instance, I recently created a migration for a new table; it looked like this:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :name, :string
t.column :email, :string
t.column :bio, :text
t.column :created_at, :datetime
t.column :updated_at, :datetime
end
end
def self.down
drop_table :chapter_comments
end
end
