Friday 1 May 2015

Usage Of Yield In Template Rendering

Without any arguments, yield will render the template of the current controller/action. So if you're on the cars/show page, it will render views/carts/show.html.erb.
When you pass yield an argument, it lets you define content in your templates that you want to be rendered outside of that template. For example, if your cars/show page has a specific html snippet that you want to render in the footer, you could add the following to your show template and the car_general layout:
<% content_for :footer do %>
  This content will show up in the footer section
<% end %>
layouts/car_general.html.erb
<%= yield :footer %>