Can someone explain the difference between "<%= render %>
" and "<%= yield %>
with <% content_for :partial do %>
/<% end %>
"? 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. THIS is the closest explanation I have found, but isn't quite clear enough for me.
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.
If theres are three views, aaa
and bbb
and ccc
, and each has an index.html.erb
, but bbb
and ccc
have a _content.html.erb
partial (signified by the underscore) how can you accomplish getting the bbb
or ccc
partial in aaa
using either render
or yield
?
The following works:
aaa
's index.html.erb :
<div">
<%= render 'bbb/content' %>
</div>
and bbb
s _content.html/erb :
<p>Content from bbb.</p>
BUT this does NOT:
aaa
's index.html.erb :
<div">
<%= yield :container %>
</div>
and bbb
s _content.html/erb :
<% content_for :container do %>
<p>Content from bbb.</p> ### viewed in aaa
<% end>
and ccc
s _content.html.erb would have nothing, or the content_for
, but I still dont get aaa
's index.html to be populated with content.
If I use the render, I can explicitly place the content in. But I thought that the benefit of using the yield :whatever
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?
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.