wusher wusher - 1 year ago 67
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download