title: What is the difference between render and yield in Rails
body: <p>Can someone explain the difference between "<code>&lt;%= render %&gt;</code>" and "<code>&lt;%= yield %&gt;</code> with <code>&lt;% content_for :partial do %&gt;</code>/<code>&lt;% end %&gt;</code>"? specifically how the routing changes when switching from one to another, the benefits of using one over the other, when is it practical to use one over the other. <a href="https://stackoverflow.com/questions/6299851/embedded-ruby-render-vs-yield">THIS</a> is the closest explanation I have found, but isn't quite clear enough for me.</p>
  
  <p>I have been trying for several days to wrap my head around this, but it seems that each configuration I try either comes close, or errors out.</p>
  
  <p>If theres are three views, <code>aaa</code> and <code>bbb</code> and <code>ccc</code>, and each has an <code>index.html.erb</code>, but <code>bbb</code> and <code>ccc</code> have a <code>_content.html.erb</code> partial (signified by the underscore) how can you accomplish getting the <code>bbb</code> or <code>ccc</code> partial in <code>aaa</code> using either <code>render</code> or <code>yield</code>?</p>
  
  <p>The following works:</p>
  
  <p><code>aaa</code>'s index.html.erb :</p>
  
  <pre><code>&lt;div"&gt;
    &lt;%= render 'bbb/content' %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and <code>bbb</code>s _content.html/erb :</p>
  
  <pre><code>&lt;p&gt;Content from bbb.&lt;/p&gt;  
  </code></pre>
  
  <p>BUT this does NOT:</p>
  
  <p><code>aaa</code>'s index.html.erb :</p>
  
  <pre><code>&lt;div"&gt;
    &lt;%= yield :container %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and <code>bbb</code>s _content.html/erb :</p>
  
  <pre><code>&lt;% content_for :container do %&gt;
    &lt;p&gt;Content from bbb.&lt;/p&gt;   ### viewed in aaa
  &lt;% end&gt;
  </code></pre>
  
  <p>and <code>ccc</code>s _content.html.erb would have nothing, or the <code>content_for</code>, but I still dont get <code>aaa</code>'s index.html to be populated with content.</p>
  
  <p>If I use the render, I can explicitly place the content in.   But I thought that the benefit of using the <code>yield :whatever</code> would allow me to choose what to populate it with, and I can't get it to populate anything as soon as I change it from render to yield.   Do I also have to update the routes file? If so, how do I choose which one to populate it with?  Does that mean its in the controller? and needs an action?</p>
  
  <p>I also have though that it depends on which file is initially routed to, but like I said, I think I need to understand the difference between the two before I can begin to use the partials to my advantage.</p>