Here I am going to share some insights on getting your Ruby on Rails site to perform better in search engines, popularly called SEO or Search Engine Optimization.
We are going to use these five Rails plugins:
- Headliner for titles
- Metamagic for meta tags
- Gretel for breadcrumbs
- FriendlyId for friendly URLs
- Dynamic Sitemaps for sitemaps
1. Titles using Headliner
Headliner is a Ruby on Rails plugin for creating titles in a <title>
tag. The plugin makes it easy to define your titles without having to define it in several places.
Titles are important in your SEO pack because they tell search engine users what content is in your individual pages.
In your application.html.erb
:
<head> <%= title :site => "My Awesome Site", :separator => "—", :reverse => true %> ... </head>
In your view:
<h1><%= title "Contact info" %></h1>
Would generate the following:
<head> <title>Contact info — My Awesome Site</title> ... </head>
2. Meta tags using Metamagic
Metamagic is a Ruby on Rails plugin for generating meta tags.
Despite what you may have heard, meta tags are still important in your SEO pack, especially the description
meta tag which Google uses in addition to the page content to both find and display your site.
In your application.html.erb
:
<head> ... <%= metamagic :title => @title %> ... </head>
(the :title => @title
part is to automatically retrieve the title from the above mentioned Headliner plugin.)
In your view:
<% meta :description => "This is my page description.", :keywords => "one, two, three" %>
Would generate the following:
<head> ... <meta name="title" content="Page title set using Headliner" /> <meta name="description" content="This is my page description." /> <meta name="keywords" content="one, two, three" /> ... </head>
3. Breadcrumbs using Gretel
Gretel is a Ruby on Rails plugin for generating breadcrumbs.
Breadcrumbs are important in your SEO pack as they tell both users and search engines the location or “path” to your pages. Google will also often display the breadcrumb instead of the page URL.
In your application.html.erb
:
<body> ... <div id="breadcrumb"> <%= breadcrumb :pretext => "You are here:", :separator => "›", :autoroot => true, :show_root_alone => false, :link_last => false, :semantic => true %> </div> ... </body>
In config/initializers/breadcrumbs.rb
:
Gretel::Crumbs.layout do crumb :root do link "Home", root_path end crumb :articles do link "Articles", articles_path end crumb :article do |article| link article.title, article_path(article) parent :articles end end
In your controller:
def show @article = Article.find(params[:id]) end
In your view:
<% breacrumb :article, @article %>
Would generate a breadcrumb like this:
<div id="breadcrumb"> You are here: <a href="/">Home</a> › <a href="/articles">Articles</a> › My Article </div>
4. Friendly URLs using FriendlyId
FriendlyId is a Ruby on Rails plugin for generating friendly URLs.
Friendly URLs are important in your SEO pack as they tell users and search engines what lies beneath the URL, e.g. /articles/34
becomes /articles/my-awesome-article
.
In your model:
class Article < ActiveRecord::Base has_friendly_id :title, :use_slug => true, :approximate_ascii => true end
In your controller:
def create @article = Article.create(:title => "My awesome article") redirect_to article_path(@article) # => /articles/my-awesome-article end
5. Sitemaps using Dynamic Sitemaps
Dynamic Sitemaps is a Ruby on Rails plugin for generating sitemaps in the sitemaps.org XML format specification.
Sitemaps are important in your SEO pack because they enable search engine crawlers to find all of your pages.
In config/initializers/sitemap.rb
:
Sitemap::Map.draw do url root_url, :last_mod => DateTime.now, :change_freq => 'daily', :priority => 1 url about_url, :change_freq => 'monthly', :priority => 0.5 url contact_url, :change_freq => 'monthly', :priority => 0.5 url terms_url, :change_freq => 'monthly', :priority => 0.5 autogenerate :articles, :last_mod => :updated_at, :change_freq => 'weekly', :priority => 0.8 end
Would generate a sitemap like this:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://yourdomain.com/</loc> <lastmod>2011-03-09</lastmod> <changefreq>daily</changefreq> <priority>1</priority> </url> ... </urlset>
If you want to notify search engines with changes to your sitemap, see the Sitemap Notifier plugin.
Conclusion
I hope this short guide will be of great use when you’re optimizing your page for search engines. If you have further tips or suggestions, please write a comment below.