title: Rails 3 vs 2.3.5 render oddities with :partial and :layout
body: <p>I've come across an oddity that I can't quite explain with regards to Rails 3 and rendering partials with layouts (from the controller). I'm hoping someone can provide a little insight into what's happening.</p>
  
  <p>First off, we'll call this controller a "legacy" controller.  It's been around for a long time and is doing a lot of things wrong, but I'm not looking to refactor it at this point so I'm trying to find ways to work with what we have.</p>
  
  <p>The <code>new</code> action is something like this (in the <code>BarsController</code>)</p>
  
  <pre><code>def new
    if something
      render :partial =&gt; "foo", :layout =&gt; "bars"
    elsif something_else
      render :partial =&gt; "foo2", :layout =&gt; "bars"
    elsif something_else_else
      render :partial =&gt; "foo3", :layout =&gt; "bars"
    else
      render :partial =&gt; "foo4", :layout =&gt; "bars"
  end
  </code></pre>
  
  <p>Now, in Rails 2.3.5, this worked fine. It would render the appropriate partial inside the appropriate layout -- I realize the layout option is redundant here as it would default to the bars layout regardless.  When we upgraded to Rails 3.0.x, we started getting errors as follows:</p>
  
  <pre><code>Missing partial layouts/bars with {:handlers=&gt;[:erb, :rjs, :builder, :rhtml, :rxml], :formats=&gt;[:html]
  </code></pre>
  
  <p>Clearly the <code>layouts/bars.html.erb</code> file is and always has been there, so I couldn't figure it out. I was able to render with <code>:layout =&gt; false</code>, but that of course wasn't going to work.   Eventually I figured out that if I do either of the following, it works:</p>
  
  <p>1) Rename my layout to <code>_bars.html.erb</code> instead of <code>bars.html.erb</code> and:</p>
  
  <p><code>render :partial =&gt; 'foo2', :layout =&gt; 'bars'</code></p>
  
  <p>2) Keep my layout as <code>bars.html.erb</code> (what I want) and :</p>
  
  <p><code>render '_foo2'  # :partial option is redundant here anyway</code></p>
  
  <hr>
  
  <p>It seems as though by using the :partial option instead of the string as first parameter is causing rails to apply the <code>_name.html.erb</code> convention to both the <code>partial</code> AND the <code>layout</code>.  If I put in the underscore on my own, it falls back to the behaviour I expected which is to not prepend an <code>_</code> infront of the name of the layout.  </p>
  
  <p>Does anyone know why this is the case?</p>
  
  <hr>
  
  <p><strong>EDIT</strong> Alright, not sure how I missed this... but here's something in the docs making mention of this.  It seems as though it's been around since 2.3.8, perhaps it was done differently in 2.3.5 (what we were running on)?</p>
  
  <blockquote>
    <p>3.4.3 Partial Layouts</p>
    
    <p>A partial can use its own layout file, just as a view can use a
    layout. For example, you might call a partial like this:</p>
    
    <p>&lt;%= render "link_area", :layout => "graybar" %> This would look for a
    partial named _link_area.html.erb and render it using the layout
    _graybar.html.erb. Note that layouts for partials follow the same
    leading-underscore naming as regular partials, and are placed in the
    same folder with the partial that they belong to (not in the master
    layouts folder).</p>
  </blockquote>
  
  <hr>