0
0
Ruby on Railsframework~20 mins

Why views present data in Ruby on Rails - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails View Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why do views in Rails present data?
In a Rails application, what is the main reason views are responsible for presenting data?
ATo separate the user interface from the business logic, making the app easier to maintain.
BTo handle database queries and data storage directly.
CTo process user input and perform calculations.
DTo manage server configuration and routing.
Attempts:
2 left
💡 Hint
Think about the role of views in showing information to users.
component_behavior
intermediate
1: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 %>
ANothing, because views cannot access instance variables.
BThe name attribute of the user with ID 1.
CAn error because @user is not defined in the view.
DThe entire user object as a string.
Attempts:
2 left
💡 Hint
Instance variables set in controllers are accessible in views.
state_output
advanced
2: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>
A<ul></ul>
B<ul><li>@products</li></ul>
CAn error because product.name is undefined.
D<ul><li>Pen</li><li>Book</li></ul>
Attempts:
2 left
💡 Hint
Each product's name is shown inside list items.
📝 Syntax
advanced
1:30remaining
Identify the correct ERB syntax for outputting data
Which option correctly outputs the variable @message in a Rails view using ERB?
A<%== @message %>
B<% @message %>
C<%= @message %>
D<%: @message %>
Attempts:
2 left
💡 Hint
ERB tags with '=' output content to the view.
🔧 Debug
expert
2: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 %>
AThe User model does not have an attribute named email_address.
BInstance variables are not accessible in views.
CThe controller did not pass @user to the view.
DThe syntax <code><%= %></code> is incorrect for outputting variables.
Attempts:
2 left
💡 Hint
Check the attribute names defined in the User model.