Codecabulary Home / Learn Rails / Partials
Partials are reusable snippets of Rails view code, meaning you can use them to DRY it out.
Install the Rails Partials Package for Sublime Text 2
-
Press command shift P
-
Select Install Package
-
Select Rails Partials
Create a Partial
-
Highlight a snippet you want to reuse
-
Press alt P
-
Name the partial
-
Sublime will add the leading underscore and filetype (.html.erb) that denotes a partial
-
If you create a partial in the layout file, it must go in app/views/application. If the folder doesn't exist, you'll likely have to move it yourself.
-
Render the partial (Sublime will also do this for you):
<%= render "partial_name" %>
Changing Loops to Partials
-
Take your standard each loop:
@events.each do |event| <td><%= event.name %></td> <td><%= event.location %></td> end
-
Remove the do/end lines:
<td><%= event.name %></td> <td><%= event.location %></td>
-
Make it a partial (Alt P, name it). In this case, I named it "event."
-
Change the render line to a hash including the collection option:
<%= render partial: "event", collection: @events %>