Advertise here!

Google Sitemap generation with Rails

Tristan at IP Angels, has a nice little post on generating Google sitemaps with Ruby on Rails. The post gives a great example of generating a sitemap for a blog. I can’t get access to the latest Typo snapshot to create a patch yet, but here’s what I think it would look like to add this into Typo.

First, edit the xml_controller.rb to add a new action, sitemap.

def sitemap
    @headers['Content-Type'] = 'text/xml; charset=utf-8'
    @articles = Article.find(:all, :conditions => 'published=1', :order => 'created_at DESC')
  end

Next, create a sitemap.rxml file in app/views/xml.

xml.instruct! :xml, :version=> '1.0', :encoding => 'UTF-8'
xml.urlset( :xmlns => 'http://www.google.com/schemas/sitemap/0.84') do
  # First entry is the main entry to the site
  xml.url do
    xml.loc server_url_for(:controller => "articles")
    xml.changefreq 'daily'
    xml.priority '0.9'
  end

  for entry in @articles
    xml.url do
      xml.loc article_url(entry, false)
      xml.changefreq 'weekly'
      xml.priority'0.5'
    end
  end
end

And remember, I just whipped this up without trying it.  So..

"Beware of bugs in the above code; I have only proved it correct, not tried it" - Donald E. Knuth

Posted at 3pm on 07/18/05 | Posted in , , , | 3 responses | read on