Generate Google sitemap with Rails and RXML
It occurred to me that a good idea would be to add a Google sitemap to Noobkit, especially because it’s using frames. Here’s how I went about it:
In a regular controller I load up all my @packages but instead of a plain sitem_map.rhtml, I have a site_map.rxml file which looks like this:
xml.instruct!
xml.urlset "xmlns" => "http://www.google.com/schemas/sitemap/0.84" do
xml.url do
xml.loc "http://test"
xml.lastmod w3c_date(Time.now)
xml.changefreq "monthly"
end
@packages.each do |package|
xml.url do
xml.loc "http://www.noobkit.com/page/ref/#{package.full_name}"
xml.lastmod w3c_date(package.created_at)
xml.changefreq "monthly"
end
end
end
w3c_date goes into your_controller_helper.rb.
def w3c_date(date)
date.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
end
That’s all to it. Packages provide entry points to their entire API trees and should be enough information for Google to start crawling there.
4 comments.
Leave a Reply