Working on my first Rails project, I used Ryan Bates' Nifty-Generators to create the initial application files. This gives me a LayoutHelper
with a title method:
def title(page_title, show_title = true)
@content_for_title = page_title.to_s
@show_title = show_title
end
In my homepage view, I have a simple title:
<%= title "Welcome" -%>
And finally, in my layout (application.html.erb
), I have:
<%- if show_title? -%>
<h1><%=h yield(:title) %></h1>
<%- end -%>
<%= yield %>
What I'm seeing is my title, "Welcome", followed by the value passed (or defaulted to) for show_title
. I want the former, but not the latter. I understand why the boolean is being implicitly returned by the method, but I didn't expect it to be displayed. The boolean display seems to be appearing as part of the primary content yield
.
The yield
command still hasn't really clicked for me yet (I get it, I just don't get it, if that makes sense), so I'm hoping someone can help me understand why the boolean is being printed and how I can make it stop. That answer may help me better understand how yield
works in addition to solving the immediate problem.
Thanks.