---
title: "Rails版本区别"
author: "yangbinfc"
date: "2011-01-19T18:05:00Z"
tags: []
slug: "rails"
draft: false
lastmod: "2012-11-04T17:48:38Z"
id: 1715395
url: "https://yangbinfc.iteye.com/blog/1715395"
---

**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 =&gt; "sqlite3"
5. config.gem"aws-s3" , :lib =&gt; "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&lt;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 =&gt;{ :preview =&gt; :post },
3. :collection =&gt;{ :archived =&gt; :get }

```
post.resources :comments,
                 :member => { :preview => :post },
                 :collection => { :archived => :get }
```

**在** **rails3中可以这样表达:**

*Ruby代码*

1. resources :comments do
2. memberdo
3. post:preview
4. end
5. collectiondo
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 =&gt; :member
3. get:archived , :on =&gt; :collection
4. end

```
resources :comments do
    post :preview, :on => :member
    get :archived, :on => :collection
end
```

**对于基本路由, rails2:**

*Ruby代码*

1. map.connect 'login' , :controller =&gt; 'session' , :action =&gt; 'new'

```
map.connect 'login', :controller => 'session', :action => 'new'
```

**那么在rails3中:**

*Ruby代码*

1. match 'login' =&gt; 'session#new'

```
match 'login' => 'session#new'
```

**对于具名路由, rails2:**

*Ruby代码*

1. map.login 'login' , :controller =&gt; 'session' , :action =&gt; 'new'

```
map.login 'login', :controller => 'session', :action => 'new'
```

**在rails3中:**

*Ruby代码*

1. match 'login' =&gt; 'session#new' , :as =&gt; :login

```
match 'login' => 'session#new', :as => :login
```

**对于程序根路由, rails2:**

*ruby代码*

1. map.root:controller=&gt; 'users' ,:action=&gt; 'index'

```
map.root :controller => 'users', :action => 'index'
```

**rails3:**

*Ruby代码*

1. root :to =&gt; 'users#index'

```
root :to => 'users#index'
```

**对于遗留路由, rails2:**

*Ruby代码*

1. map.connect ':controller/:action/:id'
2. map.connect':controller/:action/:id.:format'

```
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
```

**那么在rails3中写法更优雅:**

*Ruby代码*

1. match ':controller(/:action(/:id(.:format)))'

```
match ':controller(/:action(/:id(.:format)))'
```

**对于路由参数, rals2:**

*Ruby代码*

1. map.connect '/articles/:year/:month/:day' , :controller =&gt; 'posts' , :action =&gt; 'index'

```
map.connect '/articles/:year/:month/:day', :controller => 'posts', :action => 'index'
```

**rails3:**

*Ruby代码*

1. match '/articles/:year/:month/:day' =&gt; "posts#index"

```
match '/articles/:year/:month/:day' => "posts#index"
```

**那么对于存档请求, 比如rails2:**

*Ruby代码*

1. map.connect '/articles/:year/:month/:day' , :controller =&gt; 'posts' , :action =&gt; 'index'
2. map.connect'/articles/:year/:month' , :controller =&gt; 'posts' , :action =&gt; 'index'
3. map.connect'/articles/:year' , :controller =&gt; 'posts' , :action =&gt; 'index'

```
map.connect '/articles/:year/:month/:day', :controller => 'posts', :action => 'index'
map.connect '/articles/:year/:month', :controller => 'posts', :action => 'index'
map.connect '/articles/:year', :controller => 'posts', :action => 'index'
```

**在rails3中:**

*Ruby代码*

1. match '/articles(/:year(/:month(/:day)))' =&gt; "posts#index"

```
match '/articles(/:year(/:month(/:day)))' => "posts#index"
```

**指定请求方式, rails2:**

*Ruby代码*

1. map.connect '/articles/:year' , :controller =&gt; 'posts' , :action =&gt; 'index' ,
2. :conditions =&gt;{ :method =&gt; :get }

```
map.connect '/articles/:year', :controller => 'posts', :action => 'index',
:conditions => {:method => :get}
```

**在rails3中:**

*Ruby代码*

1. match '/articles/:year' =&gt; "posts#index" , :via =&gt; :get
2. #或者更简单的:
3. get'/articles/:year' =&gt; "posts#index"

```
match '/articles/:year' => "posts#index", :via => :get
#或者更简单的:
get '/articles/:year' => "posts#index"
```

**对于跳转, rails3:**

*Ruby代码*

1. match 'signin' , :to =&gt;redirect( "/login" )
2. match'users/:name' , :to =&gt;redirect{|params| "/#{params\[:name]}" }
3. match'google' =&gt;redirect( 'http://www.google.com/' )

```
match 'signin', :to => redirect("/login")
match 'users/:name', :to => redirect {|params| "/#{params[:name]}" }
match 'google' => redirect('http://www.google.com/')
```

**路由约束: rails2中实际上使用了 :requirements 符号**

*Ruby代码*

1. map.connect '/:year' , :controller =&gt; 'posts' , :action =&gt; 'index' ,
2. :requirements =&gt;{ :year =&gt;//d{4}/}

```
map.connect '/:year', :controller => 'posts', :action => 'index',
                                    :requirements => { :year => //d{4}/ }
```

**在rails3中:**

*Ruby代码*

1. match '/:year' =&gt; "posts#index" , :constraints =&gt;{ :year =&gt;//d{4}/}

```
match '/:year' => "posts#index", :constraints => {:year => //d{4}/}
```

*Ruby代码*

1. :constraints =&gt;{ :user\_agent =&gt;/iphone/}
2. :constraints =&gt;{ :ip =&gt;/192/.168/.1/./d{1,3}/}
3. constraints(:host =&gt;/localhost/) do
4. resources:posts
5. end
6. constraintsIpRestrictordo
7. get'admin/accounts' =&gt; "queenbee#accounts"
8. end

```
:constraints => { :user_agent => /iphone/ }
:constraints => { :ip => /192/.168/.1/./d{1,3}/ }
constraints(:host => /localhost/) do
    resources :posts
end
constraints IpRestrictor do
    get 'admin/accounts' => "queenbee#accounts"
end
```

**对于Rack应用, rails3:**

*Ruby代码*

1. get 'hello' =&gt;proc{|env|\[200,{}, "HelloRack" ]}
2. get'rack\_endpoint' =&gt;PostsController.action( :index )
3. get'rack\_app' =&gt;CustomRackApp

```
get 'hello' => proc { |env| [200, {}, "Hello Rack"] }

get 'rack_endpoint' => PostsController.action(:index)

get 'rack_app' => CustomRackApp
```

**4. Bundler与ActionController**

一个典型的rails应用, 我们一般需要在 environment.rb 指定你的 gems:

*Ruby代码*

1. config.gem "haml"
2. config.gem"chronic" , :version =&gt; '0.2.3'

```
config.gem "haml"
config.gem "chronic", :version => '0.2.3'
```

然后我们运行 $ rake gems:install, 该命令会取得并下载然后安装编译这些gems到你的系统RubyGems目录中.

之后我们运行 $ rake gems:unpack:dependencise, 把这些gem打包到你应用程序的vendor/gems目录中去.

这样做产生的问题:

1\. 它直接绑定到Rails中

2\. 没有从本质上解决依赖问题

3\. 运行时容易发生冲突

**在rails3中, 使用了 bundle 命令:**

直接在你的 gemfile 中指定你的 gem

*Ruby代码*

1. gem "haml"
2. gem"chronic" , '0.2.3'

```
gem "haml"
gem "chronic", '0.2.3'
```

然后运行 $ bundle, 该命令会会取得并下载然后安装编译这些gems

然后运行 $ bundle package 把gem源移到/vendor/cache中去.

这样rails应用中的gem与系统中的gem就不会相冲突.

一般的控制器语法:

Ruby代码

01. class UsersController&lt;ApplicationController
02. def index
03. @users =User.all
04. respond\_todo |format|
05. format.html
06. format.xml{render:xml =&gt; @users .to\_xml}
07. end
08. end
09. def show
10. @user =User.find(params\[ :id ])
11. respond\_todo |format|
12. format.html#show.html.erb
13. format.xml{render:xml =&gt; @user }
14. end
15. end
16. ...

```
class UsersController < ApplicationController
    def index
        @users = User.all
        respond_to do |format|
            format.html
            format.xml { render :xml => @users.to_xml } 
        end
    end

    def show
        @user = User.find(params[:id])
        respond_to do |format|
            format.html # show.html.erb
            format.xml { render :xml => @user }
        end
    end

...
```

改进的语法:

Ruby代码

01. class UsersController&lt;ApplicationController
02. respond\_to:html , :xml , :json
03. def index
04. @users =User.all
05. respond\_with(@users )
06. end
07. def show
08. @user =User.find(params\[ :id ])
09. respond\_with(@user )
10. end
11. ...

```
class UsersController < ApplicationController
    respond_to :html, :xml, :json
    def index
        @users = User.all
        respond_with(@users)
    end
    def show
        @user = User.find(params[:id])
        respond_with(@user)
    end
...



```

**5. ActionMailer**

rails2: $ script/generatemailerUserMailerwelcomeforgot\_password

这将创建app/models/user\_mailer.rb

那么在rails3中:$ railsgmailerUserMailerwelcomeforgot\_password

这将创建app/**mailers** /user\_mailer.rb

**在实现部分, rails2:**

*Ruby代码*

1. def welcome(user,subdomain)
2. subject'WelcometoTestApp'
3. recipientsuser.email
4. from'admin@testapp.com'
5. body:user =&gt;user, :subdomain =&gt;subdomain
6. end

```
def welcome(user, subdomain)
    subject 'Welcome to TestApp'
    recipients user.email
    from 'admin@testapp.com'
    body :user => user, :subdomain => subdomain
end
```

*Ruby代码*

1. UserMailer.deliver\_welcome(user,subdomain)

```
UserMailer.deliver_welcome(user, subdomain) 
```

**在rails3中:**

*Ruby代码*

1. def welcome(user,subdomain)
2. @user =user
3. @subdomain =subdomain
4. mail(:from =&gt; "admin@testapp.com" ,
5. :to =&gt;user.email,
6. :subject =&gt; "WelcometoTestApp" )
7. end

```
def welcome(user, subdomain)
    @user = user
    @subdomain = subdomain
    mail(:from => "admin@testapp.com",
            :to => user.email,
            :subject => "Welcome to TestApp")
end
```

*Ruby代码*

1. UserMailer.welcome(user,subdomain).deliver

```
UserMailer.welcome(user, subdomain).deliver
```

**相比rails2, 我们在rails3下实现一个mail要简单的多:**

*Ruby代码*

01. class UserMailer&lt;ActionMailer::Base
02. default:from =&gt; "admin@testapp.com" ,
03. :reply\_to =&gt; "noreply@testapp.com" ,
04. "X-Time-Code" =&gt; Time .now.to\_i.to\_s
05. def welcome(user,subdomain)
06. @user =user
07. @subdomain =subdomain
08. attachments\['test.pdf' ]= File .read( "#{Rails.root}/public/test.pdf" )
09. mail(:to =&gt; @user .email, :subject =&gt; "WelcometoTestApp" ) do |format|
10. format.html{render'other\_html\_welcome' }
11. format.text{render'other\_text\_welcome' }
12. end
13. end
14. end

```
class UserMailer < ActionMailer::Base
    default :from => "admin@testapp.com",
                :reply_to => "noreply@testapp.com",
                "X-Time-Code" => Time.now.to_i.to_s
    def welcome(user, subdomain)
        @user = user
        @subdomain = subdomain
        attachments['test.pdf'] = File.read("#{Rails.root}/public/test.pdf")
        mail(:to => @user.email, :subject => "Welcome to TestApp") do |format|
            format.html { render 'other_html_welcome' }
            format.text { render 'other_text_welcome' }
        end
    end
end
```

**6. ActiveRelation 以及 ActiveModel**

在rails2中, 我们经常使用下面的方法来进行查询:

*Ruby代码*

1. @posts =Post.find( :all , :conditions =&gt;{ :published =&gt; true })

```
@posts = Post.find(:all, :conditions => {:published => true})
```

该方式将立即查询数据库然后返回Posts数组

**而在rails3中:**

*Ruby代码*

1. @posts =Post.where( :published =&gt; true )

```
@posts = Post.where(:published => true)
```

该方法不会查询数据库, 仅仅返回一个 ActiveRecord::Relation 对象, 然后:

*Ruby代码*

1. @posts =Post.where( :published =&gt; true )
2. if params\[ :order ]
3. @posts = @posts .order(params\[ :order ])
4. end
5. @posts . each do |p|
6. ...#在这里进行查询,实现延迟加载
7. end

```
@posts = Post.where(:published => true)
if params[:order]
    @posts = @posts.order(params[:order])
end
@posts.each do |p|
    ...                 #在这里进行查询, 实现延迟加载
end
```

**对于命名范围, 在rails2中:**

*Ruby代码*

1. class Post&lt;ActiveRecord::Base
2. default\_scope:order =&gt; 'title'
3. named\_scope:published , :conditions =&gt;{ :published =&gt; true }
4. named\_scope:unpublished , :conditions =&gt;{ :published =&gt; false }
5. end

```
class Post < ActiveRecord::Base
    default_scope :order => 'title'
    named_scope :published, :conditions => {:published => true}
    named_scope :unpublished, :conditions => {:published => false}
end
```

**而在rails3中:**

*Ruby代码*

1. class Post&lt;ActiveRecord::Base
2. default\_scopeorder('title' )
3. scope:published ,where( :published =&gt; true )
4. scope:unpublished ,where( :published =&gt; false )
5. end

```
class Post < ActiveRecord::Base
    default_scope order('title')
    scope :published, where(:published => true)
    scope :unpublished, where(:published => false)
end
```

**对于查找方法, rails2:**

*Ruby代码*

1. Post.find( :all , :conditions =&gt;{ :author =&gt; "Joe" }, :includes =&gt; :comments , :order =&gt; "title" , :limit =&gt;10)
2. 在rails3:

```
Post.find(:all, :conditions => {:author => "Joe"}, :includes => :comments, :order => "title", :limit => 10)
```

*Ruby代码*

1. Post.where( :author =&gt; "Joe" ).include( :comments ).order( :title ).limit(10).&lt;strong&gt;&lt;spanstyle= "font-size:medium;" &gt;all&lt;/span&gt;&lt;/strong&gt;

```
Post.where(:author => "Joe").include(:comments).order(:title).limit(10).all



```

**7. 跨站点脚本(XSS)**

在rails2中, 一般我们输入一段文本的时候, 我们往往会这样写: &lt;%= h @post.body %&gt;

那么在rails3中, &lt;%= @post.body %&gt; 默认输出的是一段safe html, 如果想输出XSS, 可以在前面加上 raw