2005 年 2 月 15 日,星期二

路由现在可以在测试版 gem 中使用

由 admin 发布

路由是对之前称为“方向”的新名称。将职责从 mod_rewrite 移入到 Rails 中。现在这个项目已足够好,可以从在其上生长的分支移到主干和测试版 gem 中。但是,这也意味着主干和测试版 gem 目前不能直接向后兼容现有应用程序。

我们将确保有关迁移的文档对于发布版本来说非常出色,但如果你太心急了,那么以下是你对于不使用自定义 URL 的应用程序(这些应用程序的移植将更为繁琐)需要采取的几个步骤

1. 创建内容如下所示的 config/routes.rb

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. 用以下内容替换 public/.htaccess

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

ErrorDocument 500 /500.html

将新的独立的 Active Support 库添加到默认加载项。对于 gem 的安装,这意味着在 config/environment.rb 中在 require 'rubygems' 下方添加 require_gem 'activesupport'。然后代码看起来像

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

对于 SVN/tgz 安装,这意味着将 vendor/activesupport/lib 添加到 ADDITIONAL_LOAD_PATHS,并在 # Require Rails 库下添加 require 'active_support'。也同样在 config/environment.rb 中。然后代码看起来像

# 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. 在 config/environment.rb 文件中的某个位置添加 ActionController::Routing::Routes.reload

应该就是这样了。更详细的信息将在稍后提供。