Partials

A partial is a reusable piece of a view. It makes the code modular and easier to manage.

File name

File starts with _, e.g: _diploma_project.html.erb
Render the view with name only render 'diploma_project'

Example

_diploma_project.html.erb from the extens template:

<h4>Prosjektoppgave</h4>
<table>
  <tr><td>Tema:</td><td><%= topic %></td></tr>
  <tr><td>Tittel:</td><td><%= title %></td></tr>
</table>

Called from _diploma.html.erb:

<%= render 'diploma_project', topic: oppgave_tema, title: oppgave_tittel %>

Reuse

Three sections on the diploma, but with less code:

<%= render 'diploma_comment', table: merknader_person, field: "merknad", title: "Merknad" %>
<%= render 'diploma_comment', table: merknader_fag,    field: "merknad", title: "Fagmerknad" %>
<%= render 'diploma_comment', table: vedlegg_person,   field: "merknad", title: "Vedlegg" %>

Tip Sending data into a partial is optional