ActionView: templates (ERB)
ERB (Embedded Ruby) is an HTML file with small pieces of Ruby inside, and it runs from scratch on every request. This course reads real views from this project, starting with partners.html.erb. It separates the two tags: <%= %> prints a result, and <% %> runs code without output. It shows how a view reads the instance variables its action set, why Rails escapes output by default, and when html_safe overrides that. It then opens an articles#show view and reads lines from the application layout.
What you'll learn
- ✓What ERB is and how a .html.erb file renders once per request
- ✓The difference between the <%= %> and <% %> tags
- ✓Writing conditions and loops in ERB with open, print and close tags
- ✓How a view reads an action's instance variables, and why unset ones print nothing
- ✓Default output escaping, cross-site scripting and html_safe
- ✓How the layout title line and content_for use the same tags
What is ERB?
This project's roadmap page, app/views/catalog/courses/index.html.erb, shows "Continue Learning" to someone mid-course and "Start Learning" to someone who hasn't started, through its course card partial, on the same request path. A plain .html file can't do that: every visitor gets the same text.…
Read the full lesson free →Curriculum
7 lessonsWhat is ERB?
Introduces ERB as HTML with embedded Ruby, rendered from scratch on every request using the action's variables.
<%= %> vs <% %>: two tags, one difference
Separates <%= %>, which prints a result, from <% %>, which runs conditions, loops and assignments silently.
Reading an instance variable in a view
Shows a view reading @partners by name, splitting it with select and reject, and why unset variables print nothing.
Escaping and html_safe
Explains why <%= %> escapes output by default against cross-site scripting, and how html_safe opts out explicitly.
Finally opening articles#show
Opens articles/show.html.erb, which prints @article.title and branches on @article.published? using familiar ERB tags.
ERB in this project's real layout
Reads the application layout's title line and shows how a view stores that value with content_for.
Putting ERB together
Reads one snippet combining both tags, instance variables and escaping, and recaps how the course pieces fit.