阅读更多

10顶
0踩

编程语言

原创新闻 Rails 2.0 Preview Release

2007-10-04 13:41 by 见习记者 hideto 评论(0) 有7868人浏览
原文: http://weblog.rubyonrails.org/2007/9/30/rails-2-0-0-preview-release

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

2,添加routing名字空间
map.namespace(:admin) do |admin|
  admin.resources :projects,
  :collection => { :inventory => :get },
  :member => { :duplicate => :post },
  :has_many => { :tags, :images, :variants }
end

这将生成类似inventory_admin_projects_url和admin_products_tags_url的命名routes

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

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

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


Action Pack: Multiview
#respond_to得到进一步深入,对multiview使用形如action.format.renderer的模板名,如:
show.erb: 对所有formats使用同一模板
show.html.erb: html格式所使用的模板
index.atom.builder: 使用Builder渲染atom格式
edit.iphone.haml: 使用自定义HAML模板引擎对Mime::IPHONE格式渲染edit action

我们可以声明伪类型来为内部routing使用:
# 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

我们可以在config/initializers/mime_types.rb文件里声明mime-type

Action Pack: Record identification
资源routes的使用简化
# 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)


Action Pack: HTTP Loving
1,HTTP Basic Authentication的简化使用:
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


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

Action Pack: Security
1,预防CRSF攻击:
ActionController::Base.protect_from_forgery

2,预防XSS攻击:
TextHelper#sanitize

3,HTTP only cookies支持

Action Pack: Exception handling
1,rescue_action_in_public
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


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

  protected
    def deny_access
      # ...
    end
end


Action Pack: Miscellaneous
1,AtomFeedHelper
# 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


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

3,将in_place_editor和autocomplete_for变成插件

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

Active Record: Sexy migrations
# 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


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

Active Record: Shedding some weight
1,将acts_as_XYZ移到plugins

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

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

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

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

Acion Mailer
一些bug fixes以及添加assert_emails测试方法

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

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

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

And hundreds uupon hundreds of other improvements
hundreds of bug fixes

So how do I upgrade?
首先升级到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!
10
0
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

相关推荐

  • freemarker替换变量实例

    freemarker替换变量实例

  • TypeError: 'module' object is not callable(“模块”对象不可调用)

    运行结果 代码功能是生成器的调用,为什么报这个错误,“TypeError: ‘module’ object is not callable”从这串英文可以看出,模块调用出问题,在看代码调用了哪个模块,看头导入了什么模块,一个是greenlet模块一个是time模块,然而,我们导入的模块和自己程序文件命名一样,这里显然导入的是自己写的这个文件模块,不是引入的greenlet,解决办法,更改程序文...

  • TypeError: ‘module‘ object is not callable

    【报错描述】TypeError: ‘module’ object is not callable(引入自定义类) 【原因分析】模块不可用,是因为导入的模块或者实例化对象有问题 【解决办法】 ①第一种: import reader 导入的是reader这个模块(具体来说是reader.py这个文件),所以在实例化reader对象的时候需要加上模块名限定。   df = reader.read_file(’./ml-1m/ratings.dat’, sep=’::’) ② 第二种:导入reader模块的Sh.

  • 2024年最新ide和IDE系列激活(持续更新)

    【代码】2024年最新ide和IDE系列激活(持续更新)

  • TypeError: 'module' object is not callable 及解决办法

    在导入GhostNet的预训练权重时, 按照d-li14/ghostnet.pytorch的提示,我的代码: from ghostnet import ghostnet ghost = ghostnet() ghost.load_state_dict(torch.load('pretrained/ghostnet_1x-9c40f966.pth')) 出现报错:TypeError: ‘modu...

  • conda 常用的命令

    用途:用于将当前环境的配置导出到一个YAML文件中,以便在其他地方进行复制或重建环境。向配置中添加一个新的通道,使Conda在包搜索和安装时优先考虑该通道。显示当前的Conda配置信息,包括通用配置、环境配置和用户配置。用途:用于创建一个新的Conda环境,可以指定环境的名称。用途:用于在Conda仓库中搜索指定的包。用途:用于激活指定名称的Conda环境。用途:用于停用当前激活的Conda环境。用途:用于在当前环境中安装指定的包。用途:用于从当前环境中卸载指定的包。用途:用于列出当前环境中已安装的包。

  • 码率、帧率,what mean?

    以前没有接触视频这一块,对视频这一方面的知识不是很了解。现在了解一下码率和帧率的问题。       码率就是数据传输时单位时间传送的数据位数,一般我们用的单位是kbps即千位每秒。通俗一点的理解就是取样率,单位时间内取样率越大,精度就越高,处理出来的文件就越接近原始文件,但是文件体积与取样率是成正比的,所以几乎所有的编码格式重视的都是如何用最低的码率达到最少的失真,围绕这个核心衍生出来的cbr

  • FFMPEG详解(完整版)

    这些功能足以支持一个功能强大的多媒体播放器,因为最复杂的解复用、解码、数据分析过程已经在FFMpeg内部实现了,需要关注的仅剩同步问题。

  • Conda 常用命令大全(非常详细)零基础入门到精通,收藏这一篇就够了_conda命令大全

    Conda 是一个开源的软件包管理系统和环境管理系统,用于安装多个版本的软件包及其依赖关系,并在它们之间轻松切换。

  • Linux驱动学习—I2C总线

    I2C是很常见的一种总线协议,I2C是NXP公司设计的,I2C使用两条线在主控制器和从机之间进行数据通信。一条是SCL(串行时钟线),另外一条是SDA(串行数据线),因为I2C这两条数据线是开漏输出的,所以需要接上拉电阻,总线空闲的时候SCL和SDA处于高电平。I2C总线标准模式下速度可以达到100Kb/s,快速模式下可以达到400kb/s。

  • PyCharm怎么安装第三方库? Pycharm安装python库的技巧

    提示:PyCharm怎么安装第三方库?python中经常需要安装第三方库,安装的方法也有很多,今天我们就来看看使用Pycharm安装python库的技巧,详细请看下文 文章目录前言一、首先打开pycharm工具,选择File中的Setting选项,如下图所示二、在打开的setting界面中我们点击python的解释器,你会看到很多导入的第三方库,如下图所示,点击最右边的加号三.在弹出的available packages界面中,你会看到一个搜索框,如下图所示四.然后我们搜索一个插件,比如我搜索simple

  • I2C知识大全系列四 —— I2C驱动之Linux下的I2C

    本文是I2C知识大全系列文章的第四篇,介绍了Linux下的I2C驱动

  • CSS实现背景图片详解&&全屏铺满自适应的方式

    问题: CSS实现背景图片全屏铺满自适应的方式 解决: (1)background-image:可添加多张背景图片。 参数:url() || none(默认) background-image: url(images/scroll_top.jpg), url(images/scroll_bottom.jpg), url(images/scroll_middle.jpg);

  • Vimeo如何下载视频?

    Vimeo官网有下载按钮,不需要借助第三方网站或工具。 欢迎交流,www.ciyuan.org

  • 第一阶段-入门详细图文讲解tensorflow1.4 -(七)tf.estimator的IRIS

    tf.estimator是tensorflow高级API。可以很容易建立神经网络分类器。应用于iris数据集。根据萼片/花瓣的几何描述,进行分类。并且用来预测未知样本属于的花种类。 A training set of 120 samples (iris_training.csv) A test set of 30 samples (iris_test.csv).1,加载数据from

  • TensorFlow Tutorials - Images - MNIST

    TensorFlow:V1.8.0本例程中使用了TensorFlow中的高级API:EstimatorEstimator类用于训练和评估TensorFlow模型,Estimator体封装了一个由model_fn构建的模型,其中model_fn需要传入输入和一系列其他的参数,返回优化器来执行训练,评估和预测。# -*- coding: utf-8 -*- from __future__ impor...

  • android root 卸载app,如何免ROOT卸载安卓系统自带APP

    本教程需要使用adb,应用督察。本方法适用于Android7.0及以上,Android8的手机同样适用。本教程仅供参考,卸载有风险,出现问题后果自负。准备工作:windows系统以上电脑一台,数据线一根。一、分别开启开发者选项和USB调试(可能部分品牌手机设置位置不同)1.设置-&gt;关于本机-&gt;连续点击7次“版本号”,这样就开启了开发者选项。2.设置-&gt;开发者选项,开启“开启开发者...

  • Python | TypeError: ‘module’ object is not callable

    Python | TypeError: ‘module’ object is not callable

Global site tag (gtag.js) - Google Analytics