ActionView: partials
This course covers ActionView partials: view files written once and rendered wherever the same markup repeats. It uses this project's own files, such as _course_card.html.erb, _form_errors.html.erb and _sponsor_line.html.erb. Lessons explain the leading underscore in partial filenames and how Rails finds them in the calling view's folder. They show how locals are passed, why a missing local raises an error, and how local_assigns handles optional ones. They also cover rendering partials from other folders and comparing the shorthand render form with the explicit one.
What you'll learn
- ✓What a partial is and why markup is reused through it
- ✓The underscore naming rule and how Rails locates partial files
- ✓Passing locals to a partial, including collection: and as:
- ✓Reading optional locals with local_assigns
- ✓Rendering a partial from a different folder
- ✓Choosing between the shorthand and explicit render forms
What is a partial?
The roadmap page, app/views/catalog/courses/index.html.erb, renders the same course card for every course: image, title, progress bar and badge, with only the data changing. Writing that markup by hand per course would duplicate twenty-plus lines everywhere a course appears. This project writes it…
Read the full lesson free →Curriculum
7 lessonsWhat is a partial?
Introduces partials through the course card rendered for every course on the roadmap page.
The underscore naming convention
Explains the leading underscore in partial filenames and how Rails looks in the calling view's folder.
Passing local variables to a partial
Shows how locals reach a partial and why a missing expected local raises ActionView::Template::Error.
This project's real partial files
Reads three project partials that take zero, one and five locals, based on what their markup reads.
A partial in another folder
Covers rendering a partial by folder path from app/views/ and reading optional locals through local_assigns.
Shorthand vs. explicit render
Compares shorthand and explicit render, and shows when options like collection: and cached: need the explicit form.
Putting partials together
Reviews four render calls from the project and the two rules every partial in them follows.