2009年2月6日,星期五

本周 Edge Rails 动态

发布者 Mike Gunderloy

2009年1月31日 – 2009年2月6日

本周,Rails 边缘版本最大的新闻,当然是 Rails 2.3.0 RC1 的发布。自那以后,活动非常活跃,其中大部分是关于整理和根据早期采用者的 issus 和反馈修复 bug。感谢大家的测试,请务必继续保持!

Active Record 和 Action Pack 中的嵌套表单支持

这个功能非常重要,以至于它有自己的 整篇博客文章(提交的版本与您看到的有细微差别)。这个提交有两个主要部分。首先,Active Record 现在可以直接更新嵌套模型上的属性,前提是您告诉它这样做。


class Book < ActiveRecord::Base
  has_one :author
  has_many :pages

  accepts_nested_attributes_for :author, :pages
end

启用嵌套属性可以实现许多功能,包括记录及其关联子对象的自动(和原子)保存,以及子对象感知的验证。但最显眼的是嵌套表单支持。前提是父模型接受子对象的嵌套属性,您可以使用 form_forfield_for 创建嵌套表单。这些表单可以任意深度嵌套,允许您在单个视图中编辑复杂对象层次结构,而无需编写大量代码。例如,给定这个模型


class Customer < ActiveRecord::Base
  has_many :orders

  accepts_nested_attributes_for :orders, 
    :allow_destroy => true
end

在 Rails 2.3 中,您可以编写这个视图


<% form_for @customer do |customer_form| %>
  <div>
    <%= customer_form.label :name, 'Customer Name:' %>
    <%= customer_form.text_field :name %>
  </div>

  <!-- Here we call fields_for on the customer_form builder instance.
   The block is called for each member of the orders collection. -->
  <% customer_form.fields_for :orders do |order_form| %>
    <p>
      <div>
        <%= order_form.label :number, 'Order Number:' %>
        <%= order_form.text_field :number %>
      </div>

  <!-- The allow_destroy option in the model enables deletion of
   child records. -->
      <% unless order_form.object.new_record? %>
        <div>
          <%= order_form.label :_delete, 'Remove:' %>
          <%= order_form.check_box :_delete %>
        </div>
      <% end %>
    </p>
  <% end %>

  <%= customer_form.submit %>
<% end %>

有关更多信息,请参阅 Ryan Daigle 的精彩文章 Edge Rails 中的新功能:嵌套对象表单

提交

Rails Guides 重构

Rails Guides 已从使用 AsciiDoc 标记切换为使用 Textile 标记。这带来了几个好处:首先,它使贡献指南更容易。其次,这意味着我们可以放弃 Rails 树中编译的 HTML 版本指南,因为我们可以假定任何开发人员都可以从 Textile 构建。Guides 网站也有全新的外观 – 在 Beta 网站上查看。

提交

Scaffolding 更改

script/generate scaffold 生成的代码进行了一些清理 – 没有什么太大的变化,但它们使控制器和视图更符合当前的 Rails 最佳实践。同样,新 Rails 应用程序中生成的“Riding the Rails”index.html 页面现在包含指向 Rails Guides 的链接。

提交 提交 提交