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.
Hi there,
I’m trying to do the same thing for my site but I got the following error :
XML Parsing Error: mismatched tag. Expected: .
Location: http://localhost:3000/en/sitemap/sitemap
Line Number 18, Column 7:
——^
I can’t figure out where is the problem !!
Thanks in advance for your help,
Addam
Addam,
There’s no line 18 in the example, so I’m not sure what your code looks like.
Thanks for the write up.
Leave a Reply