2009 年 2 月 6 日,星期五

Rails 最新边缘版

Mike Gunderloy 发布

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

本周 Rails 最新边缘版的重大新闻当然是 Rails 2.3.0 RC1 推出。自该事件发生以来的几天里,活动很多,其中许多涉及根据早期采用者问题和反馈整理内容和修复错误。感谢参与所有测试,而且无论如何请继续关注!

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 优秀的 Rails 最新边缘版中的新内容:嵌套对象表单

提交

Rails 指南重新编写

Rails 指南已从使用 AsciiDoc 标记切换为使用 Textile 标记。这样做具有以下一些优点:首先,这使得更轻松地向指南中添加内容。其次,这意味着我们可以取消 Rails 树中指南的编译 HTML 版本,因为我们可以假设任何开发人员都可以从 Textile 构建。指南网站也有了时髦的新外观 — 在 测试版网站 看看吧。

提交

脚手架更改

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

提交 提交 提交