Google Sitemaps with Ruby on Rails
As I was recoding Codeism in Ruby on Rails I found myself looking for an easy way to create a Google Sitemap. I found templates for creating RSS and Atom feeds, but I couldn’t find one for a Google Sitemap. So here is what I did to create one.
First, Google Sitemaps uses the W3C Datetime format, so I added this method to /app/helpers/application_helper.rb:
def w3c_date(date)
date.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
end
Then I decided that rxml template was probably the easiest route to go since that’s what the RSS tempates I found used. So I created a sitemap rxml template, which you can view and download here.
As long the template is passed a valid @posts it should produce a valid Google Sitemap. You’ll also want to change YOUR_SITE_URL to the actual site URL and replace the :controller/:view/:id mapping to match your controller and view. Hope this helps and happy mapping!
