Challenge - 5 Problems
Partial Templates Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this partial render output?
Given the following Rails partial and render call, what will be the output on the page?
Ruby on Rails
<!-- _item.html.erb --> <p>Item: <%= item.name %></p> <!-- In a view --> <%= render partial: 'item', locals: { item: OpenStruct.new(name: 'Book') } %>
Attempts:
2 left
💡 Hint
Remember that locals passed to partials become local variables inside the partial.
✗ Incorrect
The partial receives the local variable 'item' with a name attribute 'Book'. It renders the paragraph with that value.
📝 Syntax
intermediate2:00remaining
Which render call correctly passes a collection to a partial?
You have a partial '_product.html.erb' expecting a local variable 'product'. Which render call correctly renders it for each product in @products?
Attempts:
2 left
💡 Hint
Look for the Rails convention to render a collection with a partial.
✗ Incorrect
Option A uses the correct 'collection' key to render the partial for each item in @products, assigning each item to 'product'.
🔧 Debug
advanced2:00remaining
Why does this partial raise an error?
Given this partial and render call, why does it raise an error?
Partial (_user.html.erb):
Name: <%= user.name %>
Render call:
<%= render 'user' %>Attempts:
2 left
💡 Hint
Check if the partial expects any local variables and if they are passed.
✗ Incorrect
The partial uses 'user' variable but the render call does not pass any locals, so 'user' is undefined causing an error.
❓ state_output
advanced2:00remaining
What is the output count of this partial rendering?
If @comments has 3 comment objects, what is the number of elements rendered by this code?
Code:
<%= render partial: 'comment', collection: @comments %>
Partial (_comment.html.erb):
<%= comment.body %> Attempts:
2 left
💡 Hint
Rendering a collection renders the partial once per item.
✗ Incorrect
Each comment in @comments renders one element, so total 3 elements appear inside the
- .
🧠 Conceptual
expert2:00remaining
What error occurs if you render a partial with a missing local variable?
Consider this partial '_profile.html.erb' that uses a local variable 'profile'. What error will Rails raise if you render it without passing 'profile'?
Ruby on Rails
<p>User: <%= profile.user_name %></p>
Attempts:
2 left
💡 Hint
Think about what happens when a variable is referenced but not defined in Ruby.
✗ Incorrect
Since 'profile' is not defined in the partial's scope, Ruby raises a NameError for undefined local variable or method.