0
0
Ruby on Railsframework~20 mins

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

Choose your learning style9 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.