● videoSpina
Creating a Sales Page: The Layout

Unlock Revisiting Ruby on Rails
Subscribe for full access to every course, or buy this one on its own.
SECTION
Spina
NEXT UP
Importing Data With Migrations
COURSE
Rails Revisited
33 lessons
About this lesson
When you work with a Repeater, you have to loop over its elements:
<span class="language-xml"><%</span><span class="language-ruby"> current\_page.content(<span class="hljs-symbol">:problem</span>).each <span class="hljs-keyword">do</span> |<span class="hljs-params">p</span>| </span><span class="language-xml">%>
<h2<%=</span><span class="language-ruby">p.content(<span class="hljs-symbol">:title</span>)</span><span class="language-xml">%></ph2
<span class="hljs-tag"><<span class="hljs-name">p</span>></span><%=</span><span class="language-ruby">p.content(<span class="hljs-symbol">:summary</span>)</span><span class="language-xml">%><span class="hljs-tag"></<span class="hljs-name">p</span>></span>
<%</span><span class="language-ruby"> <span class="hljs-keyword">end</span> </span><span class="language-xml">%></span>
You can also wrap this with an if statement if you're not sure whether a bit of content is present:
<span class="language-xml"><%</span><span class="language-ruby"> <span class="hljs-keyword">if</span>(current\_page.content(<span class="hljs-symbol">:problem</span>).present? </span><span class="language-xml">%>
...
<%</span><span class="language-ruby"> <span class="hljs-keyword">end</span> </span><span class="language-xml">%></span>
I find that working with partials is much easier, because you can plug them in where you need them. They can work like components in a Vue or React app as well.
For instance, my "hero" partial doesn't need to access the current_page stuff, you can pass the page to it, like this:
<span class="language-xml"><%=</span><span class="language-ruby"> render <span class="hljs-string">"hero"</span>, <span class="hljs-symbol">locals:</span> {<span class="hljs-symbol">page:</span> current\_page.content(<span class="hljs-symbol">:problem</span>).first } </span><span class="language-xml">%></span>
A bit verbose, but it does separate the hero from the implementation details.
Unlock Revisiting Ruby on Rails
Subscribe for full access to every course, or buy this one on its own.