#layouts/application.html.haml
%title
= yield(:title) || "Default page title"
= " | DefaultSite.com"
#application_helper.rb
def title(page_title)
content_for(:title) { page_title }
end
Even if I don’t set a title (helper method) for a page, "Default page title" never shows. Instead of it, "" (the value of yield(:title)) appears for some reason.
Why is this happening?
This is just part of the design of Rails. You can’t change this behaviour.
You should instead check if yield(:title).empty? is true or false in order to see if the content_for(:title) was called.
For example, instead of:
you want:
This just checks whether it is empty, and applies the default if it is empty, or else uses the title.