Challenge - 5 Problems
Rails View Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Why do views in Rails present data?
In a Rails application, what is the main reason views are responsible for presenting data?
Attempts:
2 left
💡 Hint
Think about the role of views in showing information to users.
✗ Incorrect
Views in Rails are designed to display data to users. They keep the user interface separate from the logic and data handling, which happens in controllers and models.
❓ component_behavior
intermediate1:30remaining
What does a Rails view render?
Given a Rails controller passes @user = User.find(1) to the view, what will the view typically render?
Ruby on Rails
<%= @user.name %>
Attempts:
2 left
💡 Hint
Instance variables set in controllers are accessible in views.
✗ Incorrect
The controller sets @user, which the view can use to show the user's name. This is how data is passed to views for display.
❓ state_output
advanced2:00remaining
Output of a view rendering a collection
What will this Rails view code output if @products = [Product.new(name: 'Pen'), Product.new(name: 'Book')]?
Ruby on Rails
<ul>
<% @products.each do |product| %>
<li><%= product.name %></li>
<% end %>
</ul>Attempts:
2 left
💡 Hint
Each product's name is shown inside list items.
✗ Incorrect
The view loops over each product and displays its name inside <li> tags, resulting in a list of product names.
📝 Syntax
advanced1:30remaining
Identify the correct ERB syntax for outputting data
Which option correctly outputs the variable
@message in a Rails view using ERB?Attempts:
2 left
💡 Hint
ERB tags with '=' output content to the view.
✗ Incorrect
The <%= %> tag outputs the value of @message. The others either do not output or are invalid syntax.
🔧 Debug
expert2:00remaining
Why does this view raise an error?
Given the controller sets
@user = User.find(params[:id]), why does this view code raise an error?<%= @user.email_address %>Attempts:
2 left
💡 Hint
Check the attribute names defined in the User model.
✗ Incorrect
If the User model uses email instead of email_address, accessing @user.email_address causes a NoMethodError.