In your views do something like this:
<% content_for :title, "Title for specific page" %>
<!-- or -->
<h1><%= content_for(:title, "Title for specific page") %></h1>
The following goes in the layout file:
<head>
<title><%= yield(:title) %></title>
<!-- Additional header tags here -->
</head>
<body>
<!-- If all pages contain a headline tag, it's preferable to put that in the layout file too -->
<h1><%= yield(:title) %></h1>
</body>
It's also possible to encapsulate the content_for
and yield(:title)
statements in helper methods (as others have already suggested). However, in simple cases such as this one I like to put the necessary code directly into the specific views without custom helpers.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…