Rails 2.0 Preview Release

原文: [url]http://weblog.rubyonrails.org/2007/9/30/rails-2-0-0-preview-release[/url]

[b]Action Pack: Resources[/b]
1,RESTful风格改进:
/people/1;edit将变成/people/1/edit

2,添加routing名字空间
[code]
map.namespace(:admin) do |admin|
admin.resources :projects,
:collection => { :inventory => :get },
:member => { :duplicate => :post },
:has_many => { :tags, :images, :variants }
end
[/code]
这将生成类似inventory_admin_projects_url和admin_products_tags_url的命名routes

3,添加"rake routes"任务,将列出通过routes.rb生成的所有命名routes

4,一个新的convention:所有基于resource的controller都默认为复数形式,这样对不同context下的map都会对应到同一controller:
[code]
# /avatars/45 => AvatarsController#show
map.resources :avatars

# /people/5/avatar => AvatarsController#show
map.resources :people, :has_one => :avatar
[/code]

[b]Action Pack: Multiview[/b]
#respond_to得到进一步深入,对multiview使用形如action.format.renderer的模板名,如:
[code]
show.erb: 对所有formats使用同一模板
show.html.erb: html格式所使用的模板
index.atom.builder: 使用Builder渲染atom格式
edit.iphone.haml: 使用自定义HAML模板引擎对Mime::IPHONE格式渲染edit action
[/code]
我们可以声明伪类型来为内部routing使用:
[code]
# should go in config/initializers/mime_types.rb
Mime.register_alias "text/html", :iphone

class ApplicationController < ActionController::Base
before_filter :adjust_format_for_iphone

private
def adjust_format_for_iphone
if request.env["HTTP_USR_AGENT"] && request.env["HTTP_USER_AGENT"][(iPhone|iPod)/]
request.format = :iphone
end
end

class PostsController < ApplicationController
def index
respond_to do |format|
format.html # renders index.html.erb
format.iphone # renders index.iphone.erb
end
end
end
[/code]
我们可以在config/initializers/mime_types.rb文件里声明mime-type

[b]Action Pack: Record identification[/b]
资源routes的使用简化
[code]
# person is a Person object, which by convention will
# be mapped to person_url for lookup
redirect_to(person)
link_to(person.name, person)
form_for(person)
[/code]

[b]Action Pack: HTTP Loving[/b]
1,HTTP Basic Authentication的简化使用:
[code]
class PostsController < ApplicationController
USER_NAME, PASSWORD = "dhh", "secret"

before_filter :authenticate, :except => [ :index ]

def index
render :text => "Everyone can see me!"
end

def edit
render :text => "I'm only accessible if you know the password"
end

private
def authenticate
authenticate_or_request_with_http_basic do |user_name, password|
user_name == USER_NAME && password == PASSWORD
end
end
end
[/code]

2,JavaScript&stylesheet文件缓存
production模式下javascript_include_tag(:all, :cache => true)将把public/javascripts/*.js弄到public/javascripts/all.js里

3,设置ActionController::Base.asset_hot = "assets%d.example.com",则image_tag等asset calls会被自动分发到asset1~asset4

[b]Action Pack: Security[/b]
1,预防CRSF攻击:
ActionController::Base.protect_from_forgery

2,预防XSS攻击:
TextHelper#sanitize

3,[url=http://msdn2.microsoft.com/en-us/library/ms533046.aspx]HTTP only cookies[/url]支持

[b]Action Pack: Exception handling[/b]
1,rescue_action_in_public
[code]
class ApplicationController < ActionController::Base
def rescue_action_in_public(exception)
logger.error("rescue_action_in_public executed")
case exception
when ActiveRecord::RecordNotFound
logger.error("404 displayed")
render(:file => "#{RAILS_ROOT}/public/404.html", :status => "404 Not Found")
# ...
end
end
[/code]

2,rescue_from
[code]
class PostsController < ApplicationController
rescue_from User::NotAuthorized, :with => :deny_access

protected
def deny_access
# ...
end
end
[/code]

[b]Action Pack: Miscellaneous[/b]
1,AtomFeedHelper
[code]
# index.atom.builder:
atom_feed do |feed|
feed.title("My great blog!")
feed.updated(@posts.first.created_at)

for post in @posts
feed.entry(post) do |entry|
entry.title(post.title)
entry.content(post.body, :type => 'html')

entry.author do |author|
author.name("DHH")
end
end
end
end
[/code]

2,asset tag调用的性能提升和简单命名routes的缓存

3,将in_place_editor和autocomplete_for变成插件

[b]Active Record: Performance[/b]
Query Cache,N+1查询的性能提升

[b]Active Record: Sexy migrations[/b]
[code]
# old
create_table :people do |t|
t.column, "account_id", :integer
t.column, "first_name", :string, :null => false
t.column, "last_name", :string, :null => false
t.column, "description", :text
t.column, "created_at", :datetime
t.column, "updated_at", :datetime
end

# new
create_table :people do |t|
t.integer :account_id
t.string :first_name, :last_name, :null => false
t.text :description
t.timestamps
end
[/code]

[b]Active Record: XML in JSON out[/b]
Person.new.from_xml("David")
person.to_json

[b]Active Record: Shedding some weight[/b]
1,将acts_as_XYZ移到plugins

2,所有商业数据库adapters移到各自的gems里,Rails仅仅自带MySQL,SQLite和PostgreSQL的adapters
商业数据库adapters的gems命名规范为activerecord-XYZ-adapter,所以可以使用gem install activerecord-oracle-adapter来安装

[b]Active Record: with_scope with a dash of syntactic vinegar[/b]
ActiveRecord::Base.with_scope成为protected以防止在controller里误用,因为它是设计来在Model里使用的

[b]Action WebService out, ActiveResource in[/b]
在SOAP vs REST的战争里,Rails选择了REST,所以Action WebService被移出为一个gem,而引入的是著名的ActiveResource

[b]ActiveSupport[/b]
添加Array#rand方法来从Array里随机得到一个元素
添加Hash#except方法来过滤不想要的keys
Date的一些扩展

[b]Acion Mailer[/b]
一些bug fixes以及添加assert_emails测试方法

[b]Rails: The debugger is back[/b]
gem install ruby-debug,然后在程序里某处使用"debugger",使用--debugger或-u来启动server即可

[b]Rails: Clean up your environment[/b]
以前各种程序的配置细节都扔在config/environment.rb里,现在我们可以在config/initializers里建立不同的文件来配置不同的选项

[b]Rails: Easier plugin order[/b]
以前plugins有依赖顺序时我们需要在config.plugins里列出来所有的plugins,现在可以这样config.plugins=[:acts_as_list, :all]

[b]And hundreds uupon hundreds of other improvements[/b]
hundreds of bug fixes

[b]So how do I upgrade?[/b]
首先升级到Rails 1.2.3,如果没有deprecation warnings,则可以升级到Rails 2.0
即将发布的Rails 1.2.4还会添加一些deprecation warnings

Thanks to everyone who’ve been involved with the development of Rails 2.0. We’ve been working on this for more than
six months and it’s great finally to be able to share it with a larger audience. Enjoy!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值