Dynamic SEO Title and Meta Tags in Rails

  • rails, ruby, gems, tutorials
  • 2 Comments

This is a simple tutorial for setting up dynamic SEO-friendly title tags and meta description tags in a Rails app. If you want your Rails app to follow SEO best practices and play nicely with web crawlers, it's crucial to have dynamic page titles and meta descriptions that complement the content on each page.

I'm going to use a basic blog app and harness Rails's dynamic templating system to automatically generate title and meta description tags from each blog's title and the first 150 characters of each blog post.

First, I'm going to create two helper methods for the title and meta description tags in app/helpers/application_helper.rb:

def title(blog_title)
  content_for(:title) { blog_title }
end

def meta_description(blog_text)
  content_for(:meta_description) { blog_text }
end

These two methods are pretty straight forward. I can pass them arguments in the form of blog_title and blog_text and call the title and meta_description methods on any view.

For each blog's show page, I want the title tag and meta description tag to properly represent the content on the page. When someone performs a web search for “dynamic seo title tags rails" I want our blog titled “Dynamic Title Tags with Rails" to have the best possible shot of appearing in the search page results. I need to grab each blog's title from the show page and pass it to the helper method as an argument. I have to do the same with the meta description which, if you recall, I am going to generate from the first 150 characters on the blog text itself. The best way to accomplish these two objectives is to call both helper methods on the posts show page template app/views/posts/show.html.erb:

<% title @post.title %>
<% meta_description @post.text.truncate(150, separator: ' ') %>

With these two lines of code I am able to grab each blog's title and the first 150 characters of each post and pass these parameters as arguments in the helper methods. This assumes of course that the Post Controller has @post = Post.find(params[:id]) in its show action.

All I have left to do is to display these dynamic features in the layout file app/views/layouts/application.html.erb. I will set up fallback content for the title and meta description tags through the use of a conditional statement since only blog show pages are covered with this technique. All other pages will default to “Ryan McMahon | Software Development | Ruby on Rails" for the title tag and “Ryan McMahon's blog about Software Engineering, Web Development, and Ruby on Rails" for the meta description:

<title> 
    <% if yield(:title).present? %>
        <%= yield(:title) %>
    <% else %>
        Ryan McMahon | Software Development | Ruby on Rails
    <% end %>
</title>
<% if yield(:meta_description).present? %>
    <meta name="description" content="<%= yield(:meta_description) %>" />
  <% else %>
    <meta name="description" content="Ryan McMahon's blog about Software Engineering, Web Development, and Ruby on Rails" />
  <% end %>