0
0
Ruby on Railsframework~20 mins

Passing data to partials in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Partial Pro
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 partial _user.html.erb and its usage, what will be rendered?

<!-- _user.html.erb -->
<p>User: <%= user.name %></p>

<!-- In a view -->
<%= render partial: 'user', locals: { user: OpenStruct.new(name: 'Alice') } %>
A<p>User: user.name</p>
B<p>User: Alice</p>
CError: undefined local variable or method `user`
D<p>User: </p>
Attempts:
2 left
💡 Hint

Remember that locals passes variables to partials as local variables.

📝 Syntax
intermediate
2:00remaining
Which option correctly passes a collection to a partial?

You want to render a list of @products using the _product.html.erb partial. Which code correctly passes the collection?

A<%= render partial: 'product', locals: { products: @products } %>
B<%= render 'product', products: @products %>
C<%= render partial: 'product', collection: @products %>
D<%= render partial: 'product', collection: @products, as: :products %>
Attempts:
2 left
💡 Hint

Use the collection: option to render a partial for each item.

🔧 Debug
advanced
2:00remaining
Why does this partial raise an error?

Consider this partial and its render call:

<!-- _item.html.erb -->
<p>Item: <%= item.title %></p>

<!-- In a view -->
<%= render partial: 'item' %>

Why does this raise an error?

ABecause the partial expects a local variable 'item' which was not passed
BBecause partials cannot be rendered without a collection
CBecause the partial name must be 'items' to match the variable
DBecause the render call is missing the 'locals:' keyword
Attempts:
2 left
💡 Hint

Check what variables the partial uses and what is passed in the render call.

state_output
advanced
2:00remaining
What is the output of this nested partial rendering?

Given these partials and render calls:

<!-- _comment.html.erb -->
<p>Comment: <%= comment.text %></p>

<!-- _post.html.erb -->
<h2><%= post.title %></h2>
<%= render partial: 'comment', collection: post.comments %>

<!-- In a view -->
<%= render partial: 'post', locals: { post: OpenStruct.new(title: 'Hello', comments: [OpenStruct.new(text: 'Nice!'), OpenStruct.new(text: 'Great!')]) } %>

What will be rendered?

A<h2>Hello</h2><p>Comment: post.comments</p>
BError: undefined local variable 'comment' in _comment.html.erb
C<h2>Hello</h2><p>Comment: </p><p>Comment: </p>
D<h2>Hello</h2><p>Comment: Nice!</p><p>Comment: Great!</p>
Attempts:
2 left
💡 Hint

Remember that collection: passes each item as a local variable named after the partial.

🧠 Conceptual
expert
2:00remaining
Which option best describes how locals work in Rails partials?

Choose the most accurate description of how locals work when rendering partials in Rails.

ALocals are variables passed explicitly to partials and are only available inside that partial's scope
BLocals are global variables accessible in all views and partials once set
CLocals automatically include all instance variables from the parent view
DLocals are used to pass data between controllers and views only
Attempts:
2 left
💡 Hint

Think about variable scope and how data is shared with partials.