This example shows a user profile page. The show.html.erb view renders the _user_info.html.erb partial and passes the @user object as user. The partial then displays the user's name and email.
Partial names start with an underscore but you omit it when rendering.
Passing data with locals keeps partials flexible and reusable.
If you don't pass data, partials can still access instance variables from the parent view.
Summary
Partials let you reuse view code in Rails.
Use locals to pass data to partials as local variables.
This makes your views cleaner and easier to maintain.
Practice
(1/5)
1. What is the main purpose of using locals when rendering partials in Rails?
easy
A. To create a new controller action
B. To include JavaScript files in the partial
C. To define CSS styles for the partial
D. To pass data as local variables to the partial
Solution
Step 1: Understand partials in Rails
Partials are reusable view templates that help keep code DRY (Don't Repeat Yourself).
Step 2: Role of locals
The locals option lets you pass data as local variables to the partial, making it flexible and dynamic.
Final Answer:
To pass data as local variables to the partial -> Option D
Quick Check:
Passing data to partials = locals [OK]
Hint: Remember: locals pass data inside partials [OK]
Common Mistakes:
Confusing locals with JavaScript or CSS inclusion
Thinking partials create controller actions
Assuming locals define styles
2. Which of the following is the correct syntax to pass a local variable named user with value @user to a partial called _profile.html.erb?
easy
A. <%= render partial: profile, user: @user %>
B. <%= render 'profile', locals: { user: @user } %>
C. <%= render profile, user: @user %>
D. <%= render partial: 'profile', locals: user: @user %>
Solution
Step 1: Identify correct render syntax with locals
The correct way to pass locals is using locals: { key: value } inside the render call.
Step 2: Check each option
<%= render 'profile', locals: { user: @user } %> uses locals: { user: @user } which is correct syntax. Others miss the locals hash or have wrong syntax.
A. Error: undefined local variable or method 'comment_text'
B. Renders the comment text correctly
C. Syntax error in render call
D. Renders nothing inside the paragraph
Solution
Step 1: Check locals passed to partial
The locals hash passes comment: @comment, so inside the partial the variable comment is available.
Step 2: Check variable used in partial
The partial uses comment_text, which is not defined or passed, causing an undefined variable error.
Final Answer:
Error: undefined local variable or method 'comment_text' -> Option A
Quick Check:
Variable name mismatch causes error [OK]
Hint: Variable names must match locals keys exactly [OK]
Common Mistakes:
Using wrong variable name inside partial
Assuming instance variables are available without passing
Not passing required locals
5. You want to render a list of products using a partial _product.html.erb and pass each product as a local variable named item. Which code correctly renders the list with locals?
hard
A. <%= render product, collection: @products, as: :item %>