Rails 2.0
posted by sean on December 7th, 2007
After months of waiting, Rails 2.0 is finally here!. If you haven't already, you can get Rails 2.0 with:
$> sudo gem update rails --include-dependencies \
--source=http://gems.rubyonrails.org
note: I had to use the '--source=http://gems.rubyonrails.org' after a few 'gem not found' errors.
Obviously, if all your applications are still running 1.2.x, there are a few caveats with just upgrading to 2.0, so be sure not to run 'gem cleanup'. If you do, you can always re-install that removed gem with:
$> sudo gem install rails --include-dependencies \
--version 1.2.x
So, let's say that umm... I were to update some existing production applications to Rails 2.0, without testing… what will break?
Where to begin? For starters, you hopefully wouldn't do that. Second, if you write good tests, run 'em. You should have plenty of output letting you know what's different. You may need to tweak a few things before your tests will even run. From the get go, there were only a few things that affected my older development applications, but most of what I've been working on lately has been release candidate copies and "preview" releases of Rails 2.0.
With my older applications, the only major issue I ran into when trying to run tests was the “undefined method `server_settings=' for ActionMailer::Base:Class” message. The method has only been renamed. Adjust the appropriate setting to read:
ActionMailer::Base.smtp_settings
A few of my favorite new features have been some of the updates made to routing. The semi-colon has been dropped from generated routes so /admin/pages/31;edit is now /admin/pages/31/edit. No more ;action_name. As trite as it sounds, that slash just looks better, and to me anyways, is much more RESTful than the semi-colon.
Route “namespaces” are definitely a powerful addition to ActionPack. Where you might have implemented a named route like:
ActionController::Routing::Routes.draw do |map|
map.resources :articles,
:path_prefix => "admin",
:name_prefix => "admin_",
:controller => "admin/articles"
end
With Rails 2.0, you can implement that same route with:
ActionController::Routing::Routes.draw do |map|
map.namespace(:admin) do |admin|
admin.resources :articles
end
end
One of the things that really got me though, was the change to named routes and how they are called from within the application. With the above example and using version 1.2.6, I would write: admin_new_article_path, but this generates an undefined method error. The correct way in Rails 2.0 is new_admin_article_path. The same applies for edit actions. You can always run rake routes to see a listing of all the routes in your application, which is an extremely helpful rake task (although I'm sure a plugin for this existed out there before 2.0, just never really looked for it).
If you had err.the_blog's Sexy Migrations plugin released in March, you know how nice it was to write much simpler migrations. Shortly after the plugin was released, it was added to the edge Rails trunk. Now, migrations are much DRYer and more efficient to write:
class CreatePages < ActiveRecord::Migration
def self.up
create_table :pages do |t|
t.integer :position, :version
t.string :title, :limit => 150
t.string :keywords, :limit =>250
t.text :content
t.timestamps
end
end
end
Notice how you can put 2 column names on one line? And the t.timestamps declaration will automatically add created_at and updated_at fields. Ahh…
Hopefully you've been keeping up over the last couple months since the preview release was announced on September 30th. If not, head on over to PeepCode and pick up the Rails 2 PDF written by Ryan Daigle (who also has a pretty sweet list of major changes and features on his site).



1 comment »
Thank you so much for this, I have been searching for ages to find a pre 2.0 how-to on namespaced urls as I can’t upgrade just yet! Works like a charm.