Ruby on Rails Sitemap: Using sitemap_generator
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:
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).
Related Guides
- WordPress Sitemap: Setup, Fix, and Submit Guide
- Shopify Sitemap: Location, Errors, and How to Submit It
- Wix Sitemap: How It Works and How to Submit It
- Squarespace Sitemap: How It Works and Common Fixes
- Webflow Sitemap: How It Works and How to Submit It
- Drupal Sitemap: XML Sitemap Module Setup Guide
- Fix Sitemap for Ruby on Rails