Bird
Raised Fist0
Ruby on Railsframework~20 mins

Partial templates in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Partial Templates Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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') } %>
A<p>Item: item.name</p>
B<p>Item: </p>
C<p>Item: Book</p>
DError: undefined local variable or method `item`
Attempts:
2 left
💡 Hint
Remember that locals passed to partials become local variables inside the partial.
📝 Syntax
intermediate
2: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?
A<%= render partial: 'product', collection: @products %>
B<%= render partial: 'product', locals: { products: @products } %>
C<%= render 'product', product: @products %>
D<%= render partial: 'product', object: @products %>
Attempts:
2 left
💡 Hint
Look for the Rails convention to render a collection with a partial.
🔧 Debug
advanced
2: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' %>
ARails does not support rendering partials without locals.
BThe partial filename is incorrect; it should be '_users.html.erb'.
CThe render call must use 'partial:' keyword explicitly.
DThe local variable 'user' is not passed, so it's undefined in the partial.
Attempts:
2 left
💡 Hint
Check if the partial expects any local variables and if they are passed.
❓ state_output
advanced
2: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 %>
  • A1 <li> element with all comments concatenated
    B3 <li> elements
    C0 <li> elements because partial is not passed a local variable
    DError due to missing 'as' option in render
    Attempts:
    2 left
    💡 Hint
    Rendering a collection renders the partial once per item.
    🧠 Conceptual
    expert
    2: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>
    ANameError: undefined local variable or method `profile`
    BSyntaxError: unexpected local variable
    CNoMethodError: undefined method `user_name` for nil:NilClass
    DArgumentError: missing required local variable 'profile'
    Attempts:
    2 left
    💡 Hint
    Think about what happens when a variable is referenced but not defined in Ruby.

    Practice

    (1/5)
    1. What is the main purpose of using partial templates in Rails views?
    easy
    A. To define routes for the application
    B. To speed up database queries
    C. To handle user authentication
    D. To reuse small pieces of view code across different pages

    Solution

    1. Step 1: Understand what partial templates are

      Partial templates are small view files that can be included in other views to avoid repeating code.
    2. Step 2: Identify the main use case

      The main use is to reuse small pieces of view code across different pages, improving maintainability.
    3. Final Answer:

      To reuse small pieces of view code across different pages -> Option D
    4. Quick Check:

      Partial templates = reuse view code [OK]
    Hint: Partial templates reuse view parts, not logic or routes [OK]
    Common Mistakes:
    • Thinking partials speed up database queries
    • Confusing partials with authentication logic
    • Assuming partials define routes
    2. Which of the following is the correct syntax to render a partial named _menu.html.erb in a Rails view?
    easy
    A. <%= partial 'menu' %>
    B. <%= render 'menu' %>
    C. <%= include 'menu' %>
    D. <%= render_partial 'menu' %>

    Solution

    1. Step 1: Recall the Rails partial rendering syntax

      Rails uses render 'partial_name' to include partials, omitting the underscore and file extension.
    2. Step 2: Match the correct syntax

      The correct syntax is <%= render 'menu' %> to render _menu.html.erb.
    3. Final Answer:

      <%= render 'menu' %> -> Option B
    4. Quick Check:

      Render partial = render 'name' [OK]
    Hint: Use render 'partial_name' without underscore or extension [OK]
    Common Mistakes:
    • Using 'partial' instead of 'render'
    • Using 'include' which is not a Rails helper
    • Adding underscore in render argument
    3. Given the following code in a Rails view:
    <%= render partial: 'product', collection: @products, as: :item %>

    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

    1. 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.
    2. Step 2: Identify the local variable name

      Here, as: :item means inside _product.html.erb each element is accessible as item.
    3. Final Answer:

      item -> Option A
    4. 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

    1. 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.
    2. Step 2: Confirm partial naming rules

      The underscore is omitted in the render call; Rails finds _user.html.erb automatically.
    3. Final Answer:

      The code is correct and will work as expected -> Option C
    4. 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 %>
    D. <%= render partial: 'comment', locals: { comment: @comments } %>

    Solution

    1. Step 1: Understand collection rendering default variable

      When rendering a collection with render partial: 'comment', collection: @comments, Rails passes each element as comment by default (partial name without underscore).
    2. 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.
    3. Final Answer:

      <%= render partial: 'comment', collection: @comments %> -> Option A
    4. 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 unknown strong while earning +1 Grip toward your certificate.

    10 questions~5 mineasy hard