I am trying to build my Rails app following the Presenter design pattern. A part of my page includes a few divs nested inside an anchor tag, which works fine using regular html, but does not work when I try to render it using ActionView helpers.
My view looks like the following:
<div class="hero-container clear">
<%= presenter.cta_background do %>
<%= presenter.header %>
<%= presenter.subheader %>
<%= presenter.button %>
<% end %>
</div>
My presenter class:
class Presenter < SimpleDelegator
def initialize(model, view)
@model, @view = model, view
super @model
end
def h
@view
end
def header
if @model.header.present?
h.content_tag :div, @model.header
end
end
def subheader
if @model.subheader.present?
h.content_tag :div, @model.subheader
end
end
def cta_button
if @model.text.present?
h.content_tag :div, @model.text
end
end
def cta_background
h.link_to @model.target do
yield
nil
end
end
end
And it ends up rendering html that looks like:
<div class="hero-container clear">
<div>Eaque ea adipisci accusantium.</div>
<div>Sed quibusdam amet et voluptatum.</div>
<div>Click here</div>
<a href="/products">products</a>
</div>
My goal is the have the three divs inside the anchor tag, which is done by passing them in a block to the link_to
function. Why is it not working when I call that block from another method like I am doing?