Renders the _article.html.erb partial for each article in the @articles collection.
Ruby on Rails
<%= render @articles %>
Sample Program
This example shows a list of articles. The index.html.erb view renders the _article.html.erb partial for each article in @articles. Each partial shows the article's title and content.
What will be the local variable name inside the _product.html.erb partial for each item?
medium
A. item
B. product
C. @products
D. collection
Solution
Step 1: Understand collection rendering with 'as'
When rendering a collection with render partial: 'name', collection: ..., as: :var, each element is passed as var inside the partial.
Step 2: Identify the local variable name
Here, as: :item means inside _product.html.erb each element is accessible as item.
Final Answer:
item -> Option A
Quick Check:
Collection with 'as' sets local variable name [OK]
Hint: Use 'as' option to rename local variable in collection partial [OK]
Common Mistakes:
Assuming default variable name 'product' instead of 'item'
Using instance variable '@products' inside partial
Confusing 'collection' as variable name
4. What is wrong with this code snippet in a Rails view?
<%= render 'user', user: @user %>
Assuming the partial is named _user.html.erb and expects a local variable user.
medium
A. The syntax should be render partial: 'user', locals: { user: @user }
B. The partial name should include the underscore: render '_user'
C. The code is correct and will work as expected
D. You cannot pass local variables to partials
Solution
Step 1: Check the shorthand syntax for passing locals
Rails allows passing locals directly by naming the variable in the render call, like render 'user', user: @user.
Step 2: Confirm partial naming rules
The underscore is omitted in the render call; Rails finds _user.html.erb automatically.
Final Answer:
The code is correct and will work as expected -> Option C
Quick Check:
Passing locals shorthand is valid [OK]
Hint: Passing locals as named args in render is valid shorthand [OK]
Common Mistakes:
Thinking underscore is required in render argument
Believing locals must be inside a hash named 'locals'
Assuming locals cannot be passed at all
5. You want to render a list of comments using a partial _comment.html.erb. Each comment should be passed as comment local variable. Which is the best way to do this in Rails?
hard
A. <%= render partial: 'comment', collection: @comments %>
B. <%= render 'comment', comments: @comments %>
C. <%= render partial: 'comment', collection: @comments, as: :comments %>
When rendering a collection with render partial: 'comment', collection: @comments, Rails passes each element as comment by default (partial name without underscore).
Step 2: Evaluate other options
<%= render 'comment', comments: @comments %> passes the whole array as a single local, not each comment. <%= render partial: 'comment', collection: @comments, as: :comments %> passes each as comments (plural). <%= render partial: 'comment', locals: { comment: @comments } %> passes the entire array as one variable, not individual comments.
Final Answer:
<%= render partial: 'comment', collection: @comments %> -> Option A
Quick Check:
Collection rendering uses partial name as local by default [OK]
Hint: Render collection with partial name to get default local variable [OK]
Common Mistakes:
Passing whole array as one local variable
Using 'as: :comments' which sets plural local name
Confusing locals hash with collection rendering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.