Rails版本区别

本文详细介绍了Rails 2到Rails 3的迁移过程,包括脚本命令的变更、配置文件的更新、路由规则的简化、ActionMailer的改进、ActiveRecord查询的优化以及跨站脚本防护的加强等核心变化。
摘要由CSDN通过智能技术生成

1. 脚本命令

旧的命令                                      新的用法

script/generate                          rails g

script/console                            rails c

script/server                              rails s

script/dbconsole                        rails db

 

 

2. 配置文件

rails2: config/environment.rb

 

Ruby代码
  1. Rails::Initializer.run  do  |config|  
  2.     config.load_paths += %W( #{RAILS_ROOT}/extras )   
  3.     config.gem "bj"   
  4.     config.gem "sqlite3-ruby":lib  =>  "sqlite3"   
  5.     config.gem "aws-s3":lib  =>  "aws/s3"   
  6.     config.plugins = [ :exception_notification  ]  
  7.     config.time_zone = 'UTC'   
  8. end   
Rails::Initializer.run do |config|
    config.load_paths += %W( #{RAILS_ROOT}/extras )
    config.gem "bj"
    config.gem "sqlite3-ruby", :lib => "sqlite3"
    config.gem "aws-s3", :lib => "aws/s3"
    config.plugins = [ :exception_notification ]
    config.time_zone = 'UTC'
end

 

 

rails3:config/application.rb

 

Ruby代码
  1. module  APP_NAME  
  2.     class  Application < Rails::Application  
  3.         config.load_paths += %W( #{RAILS_ROOT}/extras )   
  4.         config.plugins = [ :exception_notification  ]  
  5.         config.time_zone = 'UTC'   
  6.     end   
  7. end   
module APP_NAME
    class Application < Rails::Application
        config.load_paths += %W( #{RAILS_ROOT}/extras )
        config.plugins = [ :exception_notification ]
        config.time_zone = 'UTC'
    end
end

 

 这样就变成了一种架构式的应用, 我们可以根据方便的对config进行操作

 

3. 路由

在rails3中, 已经的路由可以继续工作, 而新的路由方式更加简洁.

在 rails2 中:

 

Ruby代码
  1. map.resources  :posts   do  |post|  
  2.     post.resources :comments   
  3. end   
map.resources :posts do |post|
    post.resources :comments
end

而在rails3中 , 表达更为形象:

 

Ruby代码
  1. resources  :posts   do   
  2.     resources :comments   
  3. end   
resources :posts do
    resources :comments
end

 

对于一些复杂的路由, rails2:

 

Ruby代码
  1. post.resources  :comments ,  
  2.                  :member  => {  :preview  =>  :post  },  
  3.                  :collection  => {  :archived  =>  :get  }  
post.resources :comments,
                 :member => { :preview => :post },
                 :collection => { :archived => :get }

rails3中可以这样表达:

 

Ruby代码
  1. resources  :comments   do   
  2.     member do   
  3.         post :preview   
  4.     end   
  5.     collection do   
  6.         get :archived   
  7.     end   
  8. end   
resources :comments do
    member do
        post :preview
    end
    collection do
        get :archived
    end
end

 不够简洁? 我们还可以这样做:

 

Ruby代码
  1. resources  :comments   do   
  2.     post :preview:on  =>  :member   
  3.     get :archived:on  =>  :collection   
  4. end   
resources :comments do
    post :preview, :on => :member
    get :archived, :on => :collection
end

 

对于基本路由, rails2:

 

Ruby代码
  1. map.connect  'login':controller  =>  'session':action  =>  'new'   
map.connect 'login', :controller => 'session', :action => 'new'

 那么在rails3中:


Ruby代码
  1. match  <
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值