项目中使用的Rails版本升级 2.2.2=>2.3.8

本文记录了从 Rails 2.2.2 版本升级到 2.3.8 版本的具体步骤及遇到的问题。包括修改配置文件、替换测试类、解决方法调用错误等。特别注意的是,ActionController 和 Response 类的重要变更。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中使用的Rails版本是2.2.2,想升级到到最新的2.3.8,因此做了相关的调查。调查分两方面进行:一方面是调查Rails的变更履历(在网上可以查到很多,JavaEye新闻就有,就不介绍了);一方面是在安装完Rails 2.3.8后,执行Rake测试(我们的测试Case还是很充分的,代码的覆盖率在90%以上)。

首先,要修改 environment.rb
RAILS_GEM_VERSION = '2.3.8' unless defined? RAILS_GEM_VERSION
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
...
config.action_controller.session = {
:key => '_quanp_auth_session_id', # session_key已经不推荐
:secret => secret
}
end


rake时发生了下列错误
[quote]./test/unit/../test_helper.rb:19: undefined method `use_transactional_fixtures='
for Test::Unit::TestCase:Class (NoMethodError)[/quote]
Test::Unit::TestCase 已经被要求切换为 ActiveSupport::TestCase。

xxx_controller_test中,又爆出了错误。
[quote]NoMethodError: undefined method `get' for #<ServerControllerTest:0x47cc7e4>[/quote]
需要把 class XXXControllerTest < ActiveSupport::TestCase 改成 class XXXControllerTest < ActionController::TestCase。

ActionController的变化还是蛮大的。
[list]
[*]2.2.2中Response继承的是AbstractResponse,Request继承的是AbstractRequest,而2.3.8中Response继承的是Rack::Response,Request继承的是Rack::Request。
[*]2.3.8中,如果要在TestResponse中设定header信息,可以用 @header、headers、header 三种形式,Rack::Response中有 alias :headers, :header 的定义。而2.2.2中可用的 @header[i]s[/i] 已经不能用了。
[*]ActionController::Base中的assign_default_content_type_and_charset也已经消失,需要改为调用response.assign_default_content_type_and_charset!
[*]2.2.2中如果在action中发生了RuntimeError,response的状态码是0(初始值为nil);而2.3.8中,返回的是200(初始值为200)。
[/list]
由于我们项目的代码覆写了ActionController中的一些方法,着实费了一番功夫。不过了解到了test执行时的方法调用顺序,总算是有些收获。
当在test中,执行 get :action 的时候,调用顺序如下:
[quote]ActionController::TestProcess::get
|- ActionController::TestProcess::process
| |- ActionController::ProcessWithTest::process_with_test
| | |- ActionController::Base::process
| | | |- ActionController::Base::initialize_template_class
| | | |- ActionController::Base::assign_shortcuts
| | | |- ActionController::Base::initialize_current_url
| | | |- ActionController::Base::assign_names
| | | |- ActionController::Base::log_processing
| | | |- send(method, *arguments)
| | | |- ActionController::Base::send_response
| | | | |- ActionController::Response::prepare!
| | | | | |- ActionController::Response::assign_default_content_type_and_charset!
| | | | | |- ActionController::Response::handle_conditional_get!
| | | | | |- ActionController::Response::set_content_length!
| | | | | |- ActionController::Response::convert_content_type!
| | | | | |- ActionController::Response::convert_language!
| | | | | |- ActionController::Response::convert_cookies![/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值