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.

Options

What is this?

portrait of Justin

This is the online home of Justin French, a designer & web application developer located in Melbourne, Australia. I like finding ways to make things work better. I like clarifying and simplifying. I like to understand how you understand things.

» read more

Subscribe to my feed

Follow me on Twitter

@justinfrench

More Notebook Articles

Show more notebook articles

Search