Tuesday, 6 March 2012

Ruby on Rails Page Titles

Accurate page titles are an important part of the SEO for any website. However the architecture of a modern web application relies on the content for each individual page being surrounded by a single layout to help keep the overall layout easy to maintain.

This can have a detrimental effect on SEO as Google (and the others) don’t approve of repetition of page titles as it doesn’t provide helpful information to your users. 

The Rails Approach to Page titles

Let’s setup your app so you can have custom page titles and a fall back title for those you forget.
First in your application.html.erb file (or the page you are using for the layout) add this line of text:

<title><%= content_for?(:title) ? content_for(:title) : "Beo | London based results driven web design & development" %></title>

Here are the standard <title> tags for your website with a dynamic bit of code in-between. The code looks to see if you have a custom title and if not displays the default page title (but remember this is something we are trying to avoid!).  

Next we need to add in the method referenced above, in your application_helper.rb file add this:

def title(page_title)
  content_for(:title) { page_title }

Now we need to go to each of our individual pages and insert a line of code, for example on our services.html.erb page we have:

<%- title "Beo | London based results driven web design & development | Services" %>

Again, it’s pretty simple, the code passes the hard coded title to the helper which in turn passes it to the application layout.

Go through each of your pages adding in some custom text for each title.

SEO tips from www.beo.so