By SitemapFixer Team
Updated April 2026

Ruby on Rails Sitemap: Using sitemap_generator

Validate your Rails sitemap freeCheck My Sitemap

Rails does not include sitemap generation in the framework. The most widely used gem is sitemap_generator, which integrates with Rails routing helpers, ActiveRecord models, and supports automatic pinging of Google and Bing when the sitemap is regenerated.

Setup

Add gem "sitemap_generator" to your Gemfile and run bundle install. Then generate the config file: rake sitemap:install. This creates config/sitemap.rb:

# config/sitemap.rb
SitemapGenerator::Sitemap.default_host = "https://yoursite.com"
SitemapGenerator::Sitemap.create do
add root_path, priority: 1.0, changefreq: "daily"
add about_path, priority: 0.8
Post.find_each do |post|
add post_path(post),
lastmod: post.updated_at,
priority: 0.7
end
end

Generate the sitemap with rake sitemap:refresh. This creates public/sitemap.xml.gz (compressed by default). Add public/sitemap.xml.gz and public/sitemap.xml to your .gitignore - sitemaps should be generated as part of deployment, not committed to source control.

Regenerating on Deploy

Add sitemap generation to your deployment pipeline. With Capistrano, add after "deploy:published", "sitemap:refresh" to your deploy.rb. With Heroku, add a Procfile release command or use the Heroku Scheduler add-on to run rake sitemap:refresh on a schedule. The gem can also ping Google and Bing automatically after generation - add SitemapGenerator::Sitemap.ping_search_engines after create to enable this.

Common Rails Sitemap Issues

default_host not set: Without default_host, the gem cannot generate absolute URLs and will raise a RuntimeError. Always set it to your production domain. Use ENV to manage this across environments: SitemapGenerator::Sitemap.default_host = ENV.fetch("SITE_URL", "https://yoursite.com").

Memory issues with large datasets: Using Post.all.each loads all records into memory. Use find_each or find_in_batches which processes records in chunks of 1,000 by default, keeping memory usage flat regardless of dataset size.

Sitemap hosted on S3: sitemap_generator supports uploading directly to S3 instead of the public/ directory. Configure SitemapGenerator::Sitemap.adapter with an S3 adapter if your Rails app does not serve static files directly (common with Heroku ephemeral filesystem).

Validate your Rails sitemap
Free - checks every URL in 60 seconds
Check My Sitemap Free

Related Guides