wusher wusher - 7 months ago 29
Ruby Question

Rails: How to change the title of a page?

What is the best way to create a custom title for pages in a Rails app without using a plug-in?


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:

  <title><%= yield(:title) %></title>
  <!-- Additional header tags here -->
  <!-- If all pages contain a headline tag, it's preferable to put that in the layout file too -->
  <h1><%= yield(:title) %></h1>

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.