Sitemaps in Rails, in five minutes

Not every web app needs a sitemap, but in certain cases having one can significantly improve your visibility on Google.

For instance, you might have pages that are hard to find, or that you haven’t entirely exposed to the public. You might be a brand new site that wants to get indexed as quickly as possible.

Read over Google’s reasons for having a sitemap. Take it from the authority on the matter, the ultimate “SEO expert.”

If you’re on the fence, go with a sitemap. Google states that “In most cases, webmasters benefit from sitemap submission, and in no case can you be penalized for it.”

Don’t you love the word “webmaster”?

Adding a sitemap to a small-to-medium size Rails app is a breeze. Here’s the how-to:

Step 1. Create a view for your sitemap

Your sitemap is going to be a plain old Rails view, albeit one in XML format. Below are examples in HAML and ERB.

HAML (haml-rails gem required):

A couple of important notes:

  • We’re using the post_url helper, not the post_path helper, because we need absolute URL’s in the sitemap.
  • Obviously, replace post with whatever model(s) holds the stuff you want in the sitemap.
  • If your site heavily features images and videos that you want indexed, add media metadata to your sitemap.

Step 2. Create a controller

The view above uses the @posts variable. It needs to get that from a sitemap controller:

Step 3. Add a route

Step 4. Test it out

This very simple test ensures that the sitemap is basically working, and exists mainly to ensure it doesn’t get broken in the future.

For real sitemap validation, see the next step.

Step 5. Test your sitemap and submit it to Google

Register your site on Google Webmaster Tools.

From there, you can validate and submit your sitemap for crawling.

At the end, you should see a count of the URL’s in your sitemap. You’re done!

Sitemap submission confirmation


The sitemap above is generated on-the-fly as search engines request it. If your site is particularly huge, you could generate the sitemap on a schedule. This certainly complicates things, but there are gems to help: sitemap_generator, for instance. I evaluated these but found they added too much overhead to the deployment and maintenance processes for a medium-size app.

Inspired by this Stack Exchange answer brings Rails job offers to you. Free for candidates. Salaries from $75,000 to $250,000. Sign up now!

5 thoughts on “Sitemaps in Rails, in five minutes”

  1. nice site & great content!

    just a heads up that section Step 2. Create a controller has something funny going on with the code / text


  2. I had seen a similar write up on Stack Overflow – but your explanation is more complete and worked on my first attempt to follow it. The big thing is to make sure you install the haml-rails gem and that it does not break anything else! Thanks for such a cool article!

  3. What about pages that aren’t tied directly to a model (for example text is supplied from config bars) or pages that contain data from multiple models? It seems this approach would get very complicated very quickly.

    1. That’s a great question, and I don’t have a great answer. Sitemaps are generally meant to expose pages that a search engine would otherwise have a hard time finding. They don’t necessary have to include every page on your site, so long as the pages it doesn’t include are reachable by links from other pages that it does. If you find a better way, please post here!

Leave a Reply