Bird
Raised Fist0
Ruby on Railsframework~20 mins

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

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

Practice

(1/5)
1. What is the main purpose of using locals when rendering partials in Rails?
easy
A. To create a new controller action
B. To include JavaScript files in the partial
C. To define CSS styles for the partial
D. To pass data as local variables to the partial

Solution

  1. Step 1: Understand partials in Rails

    Partials are reusable view templates that help keep code DRY (Don't Repeat Yourself).
  2. Step 2: Role of locals

    The locals option lets you pass data as local variables to the partial, making it flexible and dynamic.
  3. Final Answer:

    To pass data as local variables to the partial -> Option D
  4. Quick Check:

    Passing data to partials = locals [OK]
Hint: Remember: locals pass data inside partials [OK]
Common Mistakes:
  • Confusing locals with JavaScript or CSS inclusion
  • Thinking partials create controller actions
  • Assuming locals define styles
2. Which of the following is the correct syntax to pass a local variable named user with value @user to a partial called _profile.html.erb?
easy
A. <%= render partial: profile, user: @user %>
B. <%= render 'profile', locals: { user: @user } %>
C. <%= render profile, user: @user %>
D. <%= render partial: 'profile', locals: user: @user %>

Solution

  1. Step 1: Identify correct render syntax with locals

    The correct way to pass locals is using locals: { key: value } inside the render call.
  2. Step 2: Check each option

    <%= render 'profile', locals: { user: @user } %> uses locals: { user: @user } which is correct syntax. Others miss the locals hash or have wrong syntax.
  3. Final Answer:

    <%= render 'profile', locals: { user: @user } %> -> Option B
  4. Quick Check:

    Use locals hash to pass variables [OK]
Hint: Use locals: { var: value } to pass data [OK]
Common Mistakes:
  • Omitting the locals keyword
  • Passing variables directly without locals hash
  • Incorrect hash syntax for locals
3. Given the following code in a Rails view:
user = User.new(name: 'Alice')
<%= render 'user_info', locals: { user: user } %>

And the partial _user_info.html.erb contains:
<p>Name: <%= user.name %></p>

What will be the rendered output?
medium
A.

Name: Alice

B.

Name:

C. Error: undefined local variable 'user'
D.

Name: user

Solution

  1. Step 1: Understand locals passing

    The variable user is passed as a local variable to the partial with value User.new(name: 'Alice').
  2. Step 2: Partial uses user.name

    The partial accesses user.name, which is 'Alice', so it renders the name correctly.
  3. Final Answer:

    <p>Name: Alice</p> -> Option A
  4. Quick Check:

    Locals pass data correctly = Alice shown [OK]
Hint: Locals become variables inside partial [OK]
Common Mistakes:
  • Forgetting to pass locals causes undefined variable error
  • Assuming instance variables are automatically available
  • Confusing variable names inside partial
4. Identify the error in this code snippet:
<%= render partial: 'comment', locals: { comment: @comment } %>

# In _comment.html.erb
<p><%= comment_text %></p>

What will happen when this view is rendered?
medium
A. Error: undefined local variable or method 'comment_text'
B. Renders the comment text correctly
C. Syntax error in render call
D. Renders nothing inside the paragraph

Solution

  1. Step 1: Check locals passed to partial

    The locals hash passes comment: @comment, so inside the partial the variable comment is available.
  2. Step 2: Check variable used in partial

    The partial uses comment_text, which is not defined or passed, causing an undefined variable error.
  3. Final Answer:

    Error: undefined local variable or method 'comment_text' -> Option A
  4. Quick Check:

    Variable name mismatch causes error [OK]
Hint: Variable names must match locals keys exactly [OK]
Common Mistakes:
  • Using wrong variable name inside partial
  • Assuming instance variables are available without passing
  • Not passing required locals
5. You want to render a list of products using a partial _product.html.erb and pass each product as a local variable named item. Which code correctly renders the list with locals?
hard
A. <%= render product, collection: @products, as: :item %>
B. <%= render partial: 'product', collection: @products, locals: { item: product } %>
C. <%= render partial: 'product', collection: @products, as: :item %>
D. <% @products.each do |p| %> <%= render 'product', locals: { item: product } %> <% end %>

Solution

  1. Step 1: Understand rendering collections with locals

    Rails allows rendering a collection with render partial: 'name', collection: array, as: :var to pass each element as a local variable.
  2. Step 2: Check each option

    <%= render partial: 'product', collection: @products, as: :item %> correctly uses partial, collection, and as: :item to pass each product as item local variable. The <% @products.each do |p| %> <%= render 'product', locals: { item: product } %> <% end %> uses manual loop but product is undefined (loop var is p). The <%= render partial: 'product', collection: @products, locals: { item: product } %> has wrong locals usage (product undefined). The <%= render product, collection: @products, as: :item %> misses partial keyword and unquoted product undefined.
  3. Final Answer:

    <%= render partial: 'product', collection: @products, as: :item %> -> Option C
  4. Quick Check:

    Use collection + as for locals in partial [OK]
Hint: Use collection and as: :var for lists [OK]
Common Mistakes:
  • Passing locals inside collection render incorrectly
  • Forgetting to use as: to rename local variable
  • Using manual loops instead of collection render
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