What Is New in Ruby on Rails 2.0
| Category | Highlights |
|---|---|
| New Features | RESTful routing with resources, named routes, ActionMailer templates, page & action caching, new validation helpers (validates_presence_of, validates_numericality_of) |
| Improvements | Enhanced script/generate commands, richer test fixtures, faster ActiveRecord queries, better XML Builder support |
| Breaking Changes | Default route syntax changed; old "map.connect" patterns may need updating, controller class names must follow CamelCase |
| Deprecations | Deprecated "render :update" for RJS in favor of unobtrusive JavaScript, old "ActionWebService" removed |
| Bug Fixes | Fixed memory leak in ActiveRecord associations, corrected edge-case routing bugs, patched security issue in parameter handling |
How does Rails 2.0 enable RESTful routing and named routes?
Rails 2.0 introduces a declarative routing DSL that maps HTTP verbs to controller actions using the resources method.
- Define a resource once and get index, show, new, edit, create, update, destroy routes automatically.
- Named routes (
named_route_path) replace manual URL strings, improving readability.
# config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :articles
map.login '/login', :controller => 'sessions', :action => 'new', :as => 'login'
end
In practice this means you can call articles_path for /articles and login_path for the login page, reducing hard-coded strings across the codebase.
What improvements were made to ActionMailer in Rails 2.0?
ActionMailer now supports full template rendering and multiple delivery methods out of the box.
- Mailers are regular Ruby classes inheriting from
ActionMailer::Base. - Views live under
app/views/mailer_nameand can be ERB or plain text. - Delivery methods such as :smtp, :sendmail, and :test are configurable per environment.
# app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "[email protected]"
def welcome(user)
@user = user
mail(:to => @user.email, :subject => "Welcome")
end
end
This matters if you need to send HTML and plain-text versions of the same email without writing custom MIME handling code.
How does Rails 2.0 enhance caching mechanisms?
Rails 2.0 adds built-in page and action caching to dramatically reduce response times for static-ish content.
- Page caching stores the full HTML output as a static file, bypassing the entire Rails stack on subsequent requests.
- Action caching runs filters but caches the rendered action, allowing per-user variations via
cache_sweeper. - Cache stores can be file-system or memory-based (memcached) with a simple configuration switch.
# config/environments/production.rb
config.action_controller.perform_caching = true
config.cache_store = :mem_cache_store, "localhost"
Watch out for stale content; you must expire caches manually or hook into model callbacks to sweep them.
What new ActiveRecord validation and callback features arrived in Rails 2.0?
Rails 2.0 introduces a richer set of validation helpers and lifecycle callbacks that simplify model integrity rules.
- New helpers:
validates_presence_of,validates_numericality_of,validates_uniqueness_of. - Conditional validations using
:ifand:unlessoptions. - Additional callbacks:
before_validation,after_validation,around_save.
# app/models/post.rb
class Post < ActiveRecord::Base
validates_presence_of :title, :body
validates_numericality_of :rating, :only_integer => true, :greater_than => 0, :if => :published?
before_validation :strip_whitespace
def strip_whitespace
self.title = title.strip unless title.nil?
end
end
Most teams will find these helpers reduce custom validation code and make test suites more expressive.
Frequently Asked Questions
Can I use the old map.connect routes alongside the new resources syntax?
You can keep legacy map.connect routes, but they should be placed after resources blocks to avoid shadowing RESTful paths.
Do I need to change my existing mailer classes after upgrading to Rails 2.0?
Only if you relied on the old ActionMailer API; most mailers will work after inheriting from ActionMailer::Base.
Is page caching safe for dynamic content that changes frequently?
No, page caching is intended for content that rarely changes; use action caching or manual sweepers for frequently updated pages.
How do I enable memcached as the cache store in production?
Set config.cache_store = :mem_cache_store, "localhost" in the production environment configuration file.
What is the syntax for a conditional validation that only runs on create?
Use validates_presence_of :field, :on => :create in the model.
How can I generate a scaffold with the new script/generate command?
Run script/generate scaffold Post title:string body:text published:boolean from the terminal.