Views, ERB & Partials
Views are the V in MVC — the templates Ruby on Rails turns into the HTML your users see. Rails writes them in ERB, which lets you embed Ruby right inside HTML.
Learn Views, ERB & Partials in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll use the two ERB tags, pull data from controller instance variables, build pages with layouts, and reuse markup with partials and view helpers.
What You'll Learn in This Lesson
1️⃣ The Two ERB Tags
ERB has two main tags. <%= %> evaluates Ruby and outputs the result into the page; <% %> runs Ruby for logic (if/each) and prints nothing. The data comes from the controller's instance variables.
2️⃣ Layouts and Helpers
The layout wraps every page with shared HTML and uses <%= yield %> to insert each page's content. View helpers like link_to generate HTML for you.
A partial is a reusable fragment whose filename starts with an underscore. Render it with render to keep markup DRY.
Your turn. Choose the correct ERB tag for outputting a value versus running logic.
Mini-Challenge: Output or Logic?
For each goal, decide which ERB tag you'd reach for.
Common Errors (and the fix)
- Nothing prints — You used <% %> where you needed <%= %> to output a value.
- Double output — Putting = on an each or if can print stray values; use plain <% %> for logic.
- nil in the view — The controller didn't set the @variable ; assign it in the action.
- Partial not found — Call render "post" (no underscore) for the file _post.html.erb .
- Missing end — Every <% if %> or <% each %> needs a matching <% end %> .
Pro Tips
- 💡 Keep logic light: views should display data, not compute it — push logic to models/helpers.
- 💡 Extract partials early: repeated markup becomes a partial.
- 💡 Use helpers: link_to and friends keep your HTML correct and concise.
📋 Quick Reference — Views & ERB
Concept
Syntax
Output a value
<%= @post.title %>
Run logic
<% if ... %> ... <% end %>
Insert page content
<%= yield %>
A link
<%= link_to "Home", root_path %>
Render a partial
<%= render "post" %>
Render a collection
<%= render @posts %>
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ <%= %> outputs a value; <% %> runs logic
- ✅ Views read the controller's @ instance variables
- ✅ The layout wraps pages and uses yield
- ✅ Partials (files starting with _ ) reuse markup
- ✅ Helpers like link_to generate HTML
- ✅ Next lesson: ActiveRecord Models & Migrations
Practice quiz
What does the ERB tag <%= ... %> do?
- Runs Ruby but prints nothing
- Runs Ruby AND outputs the result into the HTML
- Only writes a comment
- Defines a route
Answer: Runs Ruby AND outputs the result into the HTML. <%= %> evaluates the Ruby and inserts its result into the page.
What does the plain <% ... %> tag (no equals) do?
- Runs Ruby for control flow but outputs nothing
- Always prints the result
- Comments out the line
- Renders a partial
Answer: Runs Ruby for control flow but outputs nothing. <% %> runs Ruby (like if/each) without printing a value — used for logic.
How does a view get data from its controller?
- By querying the database directly
- Through global variables only
- It cannot access controller data
- Through instance variables (@posts) the controller set
Answer: Through instance variables (@posts) the controller set. Instance variables assigned in the action (e.g. @posts) are available in the matching view.
What is the file extension for an HTML ERB template?
- .html.haml
- .erb.html
- .html.erb
- .rb.html
Answer: .html.erb. ERB HTML templates use the .html.erb extension, like show.html.erb.
How do you render a partial named _form.html.erb?
- partial "form"
- render "form"
- include "form"
- yield "form"
Answer: render "form". render "form" renders the partial _form.html.erb (the leading underscore is dropped in the call).
Why do partial filenames start with an underscore?
- To mark them as the layout
- Because Ruby requires it for all views
- By convention, to mark a reusable sub-template
- To disable them
Answer: By convention, to mark a reusable sub-template. The leading underscore marks a file as a partial, a reusable view fragment.
What is the application layout (app/views/layouts/application.html.erb) for?
- It defines the database schema
- It lists the routes
- It stores controller actions
- It wraps every view with shared HTML and yields the page content
Answer: It wraps every view with shared HTML and yields the page content. The layout holds shared HTML (head, nav, footer) and uses yield to insert each page's content.
Which is a view helper that builds an HTML link?
- link_to "Home", root_path
- redirect_to root_path
- render :home
Answer: link_to "Home", root_path. link_to "Home", root_path generates an anchor tag pointing at root_path.
To loop over @posts and print each title in ERB, you would use:
- A plain <% @posts.each do |post| %> ... <% end %> with <%= post.title %> inside
- <%= @posts.each %> with no block
- render @posts.title
- params.each
Answer: A plain <% @posts.each do |post| %> ... <% end %> with <%= post.title %> inside. Use <% ... %> for the each loop and <%= post.title %> to output each value.
What does <%= render @post %> typically do?
- Deletes the post
- Redirects to the post
- Runs a migration
- Renders the _post partial for that object
Answer: Renders the _post partial for that object. Passing an object to render uses the matching partial (_post.html.erb) by convention.
Continue this course
- Previous: Controllers & Actions
- Next: ActiveRecord Models & Migrations