0
0
Ruby on Railsframework~20 mins

Layouts and content_for in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Layouts and content_for Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Rails layout and view using content_for?

Given the following layout and view, what will the final HTML output be?

Layout (application.html.erb):

<html>
  <head>
    <title>MyApp</title>
    <%= yield :head %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

View (index.html.erb):

<% content_for :head do %>
  <style>body { background: #eee; }</style>
<% end %>

<h1>Welcome</h1>
<p>Hello, world!</p>
A
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;MyApp&lt;/title&gt;
    &lt;style&gt;body { background: #eee; }&lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Welcome&lt;/h1&gt;
    &lt;p&gt;Hello, world!&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
B
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;MyApp&lt;/title&gt;
    &lt;style&gt;body { background: #eee; }&lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    
  &lt;/body&gt;
&lt;/html&gt;
C
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;MyApp&lt;/title&gt;
    
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Welcome&lt;/h1&gt;
    &lt;p&gt;Hello, world!&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
D
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;MyApp&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;style&gt;body { background: #eee; }&lt;/style&gt;
    &lt;h1&gt;Welcome&lt;/h1&gt;
    &lt;p&gt;Hello, world!&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
Attempts:
2 left
💡 Hint

Remember that content_for stores content for a named block that can be rendered in the layout with yield :name.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses content_for to add a JavaScript snippet in a Rails layout?

You want to add a JavaScript snippet inside the <head> tag of your layout using content_for. Which code in your view will work correctly?

A
&lt;%= content_for :head do %&gt;
  &lt;script&gt;alert('Hi');&lt;/script&gt;
&lt;% end %&gt;
B
&lt;% content_for :javascript do %&gt;
  &lt;script&gt;alert('Hi');&lt;/script&gt;
&lt;% end %&gt;
C
&lt;% content_for :head do %&gt;
  &lt;script&gt;alert('Hi');&lt;/script&gt;
&lt;% end %&gt;
D
&lt;% content_for :head %&gt;
  &lt;script&gt;alert('Hi');&lt;/script&gt;
&lt;% end %&gt;
Attempts:
2 left
💡 Hint

Remember that content_for is a block helper and should be used with <% %> tags, not <%= %>.

state_output
advanced
2:00remaining
What is the value of content_for?(:sidebar) after rendering this view?

Given this layout and view, what will content_for?(:sidebar) return in the layout?

Layout snippet:

<body>
  <%= yield %>
  <% if content_for?(:sidebar) %>
    <aside><%= yield :sidebar %></aside>
  <% end %>
</body>

View snippet:

<h1>Page Title</h1>

<%# No content_for :sidebar block here %>
ARaises an error
Btrue
Cnil
Dfalse
Attempts:
2 left
💡 Hint

If no content was stored for a named block, content_for? returns false.

🔧 Debug
advanced
2:00remaining
Why does this content_for block not appear in the layout?

Consider this layout and view. The style block inside content_for :head does not appear in the final HTML. Why?

Layout (application.html.erb):

<html>
  <head>
    <title>Site</title>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

View (show.html.erb):

<% content_for :head do %>
  <style>body { color: red; }</style>
<% end %>

<h1>Hello</h1>
AThe layout does not call <code>yield :head</code> inside the <code>&lt;head&gt;</code> tag.
BThe <code>content_for</code> block syntax is incorrect and causes a silent failure.
CThe view must call <code>yield :head</code> to render the content.
DThe <code>content_for</code> block must be placed inside the layout, not the view.
Attempts:
2 left
💡 Hint

Check if the layout includes yield :head where the content is expected.

🧠 Conceptual
expert
3:00remaining
How does Rails handle multiple content_for calls with the same name in a single view?

In a Rails view, if you call content_for :scripts multiple times with different blocks, what will yield :scripts output in the layout?

AOnly the first <code>content_for :scripts</code> block content is output, later ones are ignored.
BAll <code>content_for :scripts</code> blocks are concatenated in the order they appear and output together.
CRails raises an error because multiple <code>content_for</code> blocks with the same name are not allowed.
DOnly the last <code>content_for :scripts</code> block content is output, previous ones are overwritten.
Attempts:
2 left
💡 Hint

Think about how Rails accumulates content for named blocks.