Tuesday, February 15, 2005

Routing now available in beta gems

Posted by admin

Routing is the new name for what was previously known as Directions. The move of responsibility from mod_rewrite and into Rails. This project is now good enough to have moved off the branch it was growing on and into the trunk and beta gems. This also means, however, that the trunk and the beta gems are currently not directly backwards compatible with existing applications.

We’re going to make sure that the migrating documentation is superb for release, but if you’re too impatient, here are the few steps you need to take on applications that doesn’t use custom URLs (those will be a bit more cumbersome to port):

1. Create config/routes.rb with the following content:

ActionController::Routing::Routes.draw do |map|
  # Add your own custom routes here.
  # The priority is based upon order of creation: first created -> highest priority.
  
  # Here's a sample route:
  # map.connect 'products/:id', :controller => 'catalog', :action => 'view'
  # Keep in mind you can assign values other than :controller and :action
  
  # Install the default route as the lowest priority.
  map.connect ':controller/:action/:id'
end

2. Replace public/.htaccess with:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L]

ErrorDocument 500 /500.html

3. Add the new independent Active Support library to the default loads. For gem installations, it means adding require_gem 'activesupport' right underneath require 'rubygems' in config/environment.rb. It’ll then look like:

# Require Rails gems.
require 'rubygems'
require_gem 'activesupport'
require_gem 'activerecord'
require_gem 'actionpack'
require_gem 'actionmailer'
require_gem 'rails'

For SVN/tgz installations, it means adding vendor/activesupport/lib to the ADDITIONAL_LOAD_PATHS and add require 'active_support' under # Require Rails libraries. also in config/environment.rb. It’ll then look like:

# Followed by the standard includes.
ADDITIONAL_LOAD_PATHS.concat %w(
  app
  app/models
  app/controllers
  app/helpers
  config
  lib
  vendor
  vendor/railties
  vendor/railties/lib
  vendor/activesupport/lib
  vendor/activerecord/lib
  vendor/actionpack/lib
  vendor/actionmailer/lib
).map { |dir| "#{RAILS_ROOT}/#{dir}" }

# Prepend to $LOAD_PATH
ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) }


# Require Rails libraries.
require 'active_support'
require 'active_record'
require 'action_controller'
require 'action_mailer'

4. Add ActionController::Routing::Routes.reload somewhere in your config/environment.rb file.

That should be it. More detailed information will follow shortly.