This post originated from an RSS feed registered with Ruby Buzz
by rodney ramdas.
Original Post: Google Sitemaps Rails plugin
Feed Title: pinupgeek.com
Feed URL: http://feeds.feedburner.com/pinupgeek
Feed Description: A personal take on Ruby and Rails
I was creating a Google Sitemaps implementation for a customer when it occurred to me sitemaps probably
can be put in a nice plugin as long as we follow some conventions. Luckily REST gave us those conventions
and so here is the sitemap_generator plugin:
script/generate sitemap jobs daily
will generate a jobs/sitemap.xml for you:
xml.instruct!
xml.urlset "xmlns" => "http://www.google.com/schemas/sitemap/0.84" do
xml.url do
xml.loc "http://#{@host}/jobs"
xml.lastmod w3c_date(@jobs.first.updated_at)
xml.changefreq "daily"
end
@jobs.each do |job|
xml.url do
xml.loc "http://#{@host}/jobs/#{job.id}"
xml.lastmod w3c_date(job.updated_at)
xml.changefreq "daily"
end
end
end
That builder template results in something like this:
Valid frequencies are specified by Google. Be sure to look through the specs. Apart from the above, it also generates a Google SitemapIndex file for you in /sitemap.xml (i.e the file that lists all your sitemaps).
It works because if you’re using REST the plugin can make some assumptions
about how your app is structured. I think in 90% of the cases this sitemap_generator is all you’re going to need.
For the rest you’re on your own but you can use the generator as a nice startup since it gives you all the necessary ingredients.
Incidentally, my customer’s wishes we’re a bit more exotic than this plugin could deliver so I didn’t end up using this plugin. But it would be a shame to toss it out the window really so here it is:
svn co svn://www.buildless.com/svn/repos/sitemap_generator
in your vendor/plugins directory and follow the instructions. Feedback is more than welcome.