Custom Page Titles in Rails
Meaningful page titles are important as they can weigh heavily in your search engine ranking. Here’s a quick tip for adding custom page titles to your Rails app.
In the controller, create a @pagetitle instance variable on any actions that you wish to have a custom title.
class CountriesController < ApplicationController
layout "big"
def index
@country= Country.find(:all)
@pagetitle = "All Countries"
end
end
And then in the view, add the following:
<title>
<% if @pagetitle %>
<%= @pagetitle %> at
<% else %>
your tag line at
<% end %>
yoururl.com
</title>
Obviously you should change ‘your tag line’ and ‘yoururl.com’ to suit. I read a similar post on this, which used the slightly more elegant:
<%= @pagetitle || "your tag line at your url.com">
The only problem I can see with this is that you can’t add a constant suffix to your page titles, e.g. “all countries at yoururl.com”, “something else at yoururl.com”, but in reality you could user either. Happy titleling.

Leave a Reply