<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Turning Point &#187; Rails</title>
	<atom:link href="http://blog.franktrindade.com/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.franktrindade.com</link>
	<description>Agile, software and some non-sense</description>
	<lastBuildDate>Tue, 04 May 2010 09:00:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Rails 2.3 New Features</title>
		<link>http://blog.franktrindade.com/2009/02/02/rails-23-new-features/</link>
		<comments>http://blog.franktrindade.com/2009/02/02/rails-23-new-features/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 12:21:18 +0000</pubDate>
		<dc:creator>franktrindade</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[rails 2.3]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://blog.franktrindade.com/?p=199</guid>
		<description><![CDATA[In the set of new features from Rails 2.3, I was quite pleased by two of them: nested attributes and nested forms. Both allow the creation of forms containing information about more than just one model, and may solve a recurrent problem I had in past projects, which has lead to some painful headaches&#8230; Can&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>In the set of new features from <a href="http://guides.rubyonrails.org/2_3_release_notes.html" target="_blank">Rails 2.3</a>, I was quite pleased by two of them: nested attributes and nested forms.</p>
<p>Both allow the creation of forms containing information about more than just one model, and may solve a recurrent problem I had in past projects, which has lead to some painful headaches&#8230;</p>
<p>Can&#8217;t wait to try it!</p>
<p>More information (<a href="http://guides.rubyonrails.org/2_3_release_notes.html">source</a>):</p>
<h3 id="_nested_attributes">3.1. Nested Attributes</h3>
<div class="paragraph">
<p>Active Record can now update the attributes on nested models directly, provided you tell it to do so:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9 by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --></p>
<pre><tt><span style="font-weight: bold;"><span style="color: #0000ff;">class</span></span> Book <span style="color: #990000;">&lt;</span> ActiveRecord<span style="color: #990000;">::</span>Base
  has_one <span style="color: #990000;">:</span>author
  has_many <span style="color: #990000;">:</span>pages

  accepts_nested_attributes_for <span style="color: #990000;">:</span>author<span style="color: #990000;">,</span> <span style="color: #990000;">:</span>pages
<span style="font-weight: bold;"><span style="color: #0000ff;">end

</span></span></tt></pre>
<h3 id="_nested_object_forms">5.1. Nested Object Forms</h3>
<div class="paragraph">
<p>Provided the parent model accepts nested attributes for the child objects (as discussed in the Active Record section), you can create nested forms using <tt>form_for</tt> and <tt>field_for</tt>. These forms can be nested arbitrarily deep, allowing you to edit complex object hierarchies on a single view without excessive code. For example, given this model:</div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9 by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --></p>
<pre><tt><span style="font-weight: bold;"><span style="color: #0000ff;">class</span></span> Customer <span style="color: #990000;">&lt;</span> ActiveRecord<span style="color: #990000;">::</span>Base
  has_many <span style="color: #990000;">:</span>orders

  accepts_nested_attributes_for <span style="color: #990000;">:</span>orders<span style="color: #990000;">,</span> <span style="color: #990000;">:</span>allow_destroy <span style="color: #990000;">=&gt;</span> <span style="font-weight: bold;"><span style="color: #0000ff;">true</span></span>
<span style="font-weight: bold;"><span style="color: #0000ff;">end</span></span></tt></pre>
</div>
</div>
<div class="paragraph">
<p>You can write this view in Rails 2.3:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9 by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --></p>
<pre><tt><span style="color: #ff0000;">&lt;% form_for @customer do |customer_form| %&gt;</span>
  <span style="color: #ff0000;">&lt;div&gt;</span>
    <span style="color: #ff0000;">&lt;%= customer_form.label :name, 'Customer Name:' %&gt;</span>
    <span style="color: #ff0000;">&lt;%= customer_form.text_field :name %&gt;</span>
  <span style="color: #ff0000;">&lt;/div&gt;</span>

  <span style="color: #990000;">&lt;!--</span> Here we call fields_for on the customer_form
       builder instance<span style="color: #990000;">.</span> The block is called <span style="font-weight: bold;"><span style="color: #0000ff;">for</span></span> each
       member of the orders collection<span style="color: #990000;">.</span> <span style="color: #990000;">--&gt;</span>
  <span style="color: #ff0000;">&lt;% customer_form.fields_for <img src='http://blog.franktrindade.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rders do |order_form| %&gt;</span>
      <span style="color: #ff0000;">&lt;p&gt;</span>
        <span style="color: #ff0000;">&lt;div&gt;</span>
          <span style="color: #ff0000;">&lt;%= order_form.label :number, 'Order Number:' %&gt;</span>
          <span style="color: #ff0000;">&lt;%= order_form.text_field :number %&gt;</span>
        <span style="color: #ff0000;">&lt;/div&gt;</span>

  <span style="color: #990000;">&lt;!--</span> The allow_destroy option <span style="font-weight: bold;"><span style="color: #0000ff;">in</span></span> the model
       enables deletion of child records<span style="color: #990000;">.</span> <span style="color: #990000;">--&gt;</span>
        <span style="color: #ff0000;">&lt;% unless order_form.object.new_record? %&gt;</span>
          <span style="color: #ff0000;">&lt;div&gt;</span>
            <span style="color: #ff0000;">&lt;%= order_form.label :_delete, 'Remove:' %&gt;</span>
            <span style="color: #ff0000;">&lt;%= order_form.check_box :_delete %&gt;</span>
          <span style="color: #ff0000;">&lt;/div&gt;</span>
        <span style="color: #ff0000;">&lt;% end %&gt;</span>
      <span style="color: #ff0000;">&lt;/p&gt;</span>
    <span style="color: #ff0000;">&lt;% end %&gt;</span>
  <span style="color: #ff0000;">&lt;% end %&gt;</span>

  <span style="color: #ff0000;">&lt;%= customer_form.submit %&gt;</span>
<span style="color: #ff0000;">&lt;% end %&gt;</span></tt></pre>
</div>
</div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.franktrindade.com/2009/02/02/rails-23-new-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
