What Is New in Ruby on Rails 1.1
| Category | Highlights |
|---|---|
| New Features | Action Mailer, RESTful routing helpers, page & fragment caching, improved generators |
| Improvements | Scaffolding now respects custom templates, faster template rendering, better Ruby 1.8.2 compatibility |
| Bug Fixes | Fixed memory leak in ActiveRecord, corrected edge-case routing bugs, resolved several ActionView rendering issues |
| Breaking Changes | Removed deprecated :include option from find, renamed :conditions hash key to :where, default session store switched to cookie-based |
| Deprecations | Old style observers, :validate_on_create/:validate_on_update callbacks, and the :scaffold_controller generator |
How does Rails 1.1 improve email handling?
Rails 1.1 introduces Action Mailer, a dedicated framework for building and delivering email messages.
- Mailers are plain Ruby classes that inherit from
ActionMailer::Base. - Templates can be written in ERB or Haml, just like normal views.
- Delivery methods (SMTP, sendmail, test) are configurable per environment.
class UserMailer < ActionMailer::Base
default from: "[email protected]"
def welcome_email(user)
@user = user
mail(to: @user.email, subject: "Welcome to MyApp")
end
end
In practice, this means you can generate a mailer with script/generate mailer UserMailer and keep email logic out of controllers.
What routing enhancements are available in Rails 1.1?
Rails 1.1 adds RESTful routing helpers and named routes that make URL generation explicit and testable.
map.resources :articlesautomatically creates index, show, new, edit, create, update, destroy routes.- Named routes via
map.named_route 'profile/:id', :controller => 'users', :action => 'show'generateprofile_path(@user). - Route constraints and optional segments are now supported.
# config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :posts
map.profile '/users/:id', :controller => 'users', :action => 'show', :as => :profile
end
This matters if your team is moving toward a resource-oriented API; the generated helpers reduce hard-coded URL strings.
Which caching mechanisms were added or refined in Rails 1.1?
Rails 1.1 introduces page caching and fragment caching, giving developers fine-grained control over what gets cached.
- Page caching: stores the full response as a static file; use
caches_page :action_name. - Fragment caching: caches reusable view snippets with
cache do ... endblocks. - Cache store defaults now point to the file system, but you can plug in memcached via
config.action_controller.cache_store = :mem_cache_store.
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
caches_page :show
caches_action :index, :expires_in => 5.minutes
end
# app/views/articles/_summary.html.erb
<% cache @article do %>
<%= @article.title %>
<%= truncate(@article.body, :length => 200) %>
<% end %>
Watch out for stale content after updates; you must manually expire cached pages with expire_page or expire_fragment.
What generator and scaffolding updates should I expect in Rails 1.1?
Rails 1.1 refines the generator system, allowing custom templates and more granular scaffolding options.
- Generators now accept
--skipflags to omit assets, tests, or helpers. - Custom generator templates can be placed under
lib/templatesand will be picked up automatically. - Scaffolded controllers now use RESTful actions by default.
# Generate a scaffold without JavaScript helpers
script/generate scaffold Post title:string body:text --skip-javascript
# Create a custom model generator
mkdir -p lib/templates/model
cp $(rails_path)/generators/model/model.rb lib/templates/model/model.rb
Most teams will appreciate the ability to tailor generated code to their style guide, reducing post-generation cleanup.
Frequently Asked Questions
Can I use Action Mailer with an existing SMTP server?
You can configure Action Mailer to point at any SMTP host by setting :address, :port, :user_name and :password in the environment config.
Do the new RESTful routes break existing URL helpers?
Only if you relied on the old wildcard routes; the named helpers generated by map.resources replace them.
Is page caching compatible with SSL sites?
Page caching works with SSL but you must ensure the cached files are served over HTTPS to avoid mixed-content warnings.
How do I expire a fragment cache after an update?
Call expire_fragment with the same cache key used in the view, for example expire_fragment(@article).
What command generates a new mailer class?
You can generate a mailer with script/generate mailer UserMailer.
Will my existing scaffolds need to be regenerated after upgrading?
Most scaffolds will continue to work, but you may want to regenerate them to take advantage of the new RESTful actions and skip options.