Skip to content

Instantly share code, notes, and snippets.

@zaiste
Last active May 3, 2018 11:21
Show Gist options
  • Save zaiste/10dd50ed913d846a16347f9fd2a8f9a4 to your computer and use it in GitHub Desktop.
Save zaiste/10dd50ed913d846a16347f9fd2a8f9a4 to your computer and use it in GitHub Desktop.
Rails Partials with Blocks

Partials in Rails can accept blocks. A block is a custom piece of markup. As with regular Ruby blocks, you nest the markup within do...end section of the render function.

<%= render layout: 'nav' do %>
  <li><%= link_to 'Books', books_path %></li>
  <li><%= link_to 'CDs', cds_path %></li>
  <li><%= link_to 'Games', games_path %></li>
<% end %>

Nesting works only if render specifies the partial with the layout: option instead of the usual partial:.

The partial definition uses yield to mark the location where the markup from the passed block will be inserted.

<ul class="navigation">
  <%= yield %>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment