In one of our app, the content_for is used to set the page title from a helper method. It looks something like this:

def title(*parts)
  unless parts.empty?
    content_for :title do
      parts.unshift(app_name).join(" - ")
    end
  end
end

which when used in a view template like so:

<% title('Blog') %>;

will return the title

Site - Blog

We use monologue for our blog engine. Monologue is for a fact also setting the content for :title which resulted in a real mess!

Why that? The reason is pretty simple: When calling multiple time the content_for method for a given key, Rails will append their body together. What seems to be a sensible default implementation did not fit our title requirements.

It is possible to delete the content defined for a given key like so:

@view_flow.content.delete :title

Which I simply added in our title method above:

def title(*parts)
  unless parts.empty?
    @view_flow.content.delete :title
    content_for :title do
      parts.unshift(app_name).join(" - ")
    end
  end
end

And that’s it… our titles are shining again!