Default Page Titles in Rails
Published 3 October 05 by Justin French, 4 comments
In this case, someone on the Rails list was asking how to set the page title in a controller, which is fine, but I’ve found it much easier to do this in a layout:
<title><%= @page_title || "Default Title Here" %></title>
Utterly simple? Sure. But because Rails compiles the layout and view at the same time, you can assign a value to @page_title in your views, which makes perfect sense, given that the layout is conceptually part of the view.
So if I set <% @page_title = "Something Else" -%> in my view, the layout will pick it up and display that text in the title bar. Too easy.
This interaction between the layout and view can be used in many ways, like setting content for a keyword/description/copyright meta tag.
Before you go…
Here’s some links to my most popular posts: