Designing a Sitemap for Rails without Gems
Dynamic sitemaps are very easy to build in rails and can vastly improve your organic SEO. In just 10 minutes, your web application can have a dynamic sitemap that is always up-to-date.
Let me first give props to the sitemap_generator
gem. This is a great project and useful in many situations, but sometimes it’s better to just build your sitemap from scratch. One of the biggest drawbacks is that it can’t be used on Heroku, unless you want to host the sitemap on a cloud service like S3 with carrierwave and fog - that’s at least 3 new gems just for a sitemap. To me, this seems like a lot of overhead for a relatively minor concern. In this lesson, I’ll show you how to build a simple sitemap without any external dependencies. We will also setup an simple rake task to ping Google or any other search engine.
Here's What We're Going to Code
- A Ruby on Rails view and controller that will format XML to match Google's sitemap guidelines.
- A custom rake task to ping google when our sitemap changes.
Other Important Info
- This tutorial uses Rails 4.2 and Ruby 2.2 (but works with any version of rails 4+)
- You should know the basics of Rails before starting this lesson.
- I'm offering this lesson for free!
Correct XML Format for Sitemaps
According to Google’s Sitemap guidelines, this is the preferred XML markup for a sitemap.
<?xml version=“1.0” encoding=“UTF-8”?>
<urlset xmlns=“http://www.sitemaps.org/schemas/sitemap/0.9”>
<url>
<loc>https://www.codediode.io</loc>
</url>
<url>
<loc>https://www.codediode.io/about</loc>
</url>
</urlset>
Creating the XML Sitemap Controller and Views in Rails
This Section is Locked!
Unlock this lesson for free to view all sections.
Rake Task to Ping Google or Other Search Engines
This Section is Locked!
Unlock this lesson for free to view all sections.
Grades
Graded
Worked great. Just implemented this in my rails app and it built the sitemap just as expected. I was also glad to see it was accepted by google without warnings or issues. Thanks.

- 35 Unlocks
- 944 Total Reads
- 16 minutes Est. Learning Time
Graded
Years later, and I was still able to implement this in a Rails 5.1 app. Thank you.