Satchel
Satchel

Reputation: 16724

recommended way or plugin to create google sitemaps for ruby on rails app?

I did a quick Google search and didn't see anything super-great to automate creation and updating of my google sitemap for the ruby on rails app. Any suggestions?

Upvotes: 3

Views: 2021

Answers (4)

Karl Varga
Karl Varga

Reputation: 304

I would recommend that you check out the sitemap_generator gem. It handles all of these issues for you...and really, who wants to mess around authoring XML?

Here is an example sitemap to show how you use your Rails models and path helpers to generate your sitemap URLs:

# config/sitemap.rb
SitemapGenerator::Sitemap.default_host = "http://www.example.com"
SitemapGenerator::Sitemap.create do
  add '/contact_us'
  Content.find_each do |content|
    add content_path(content), :lastmod => content.updated_at
  end
end

Then you use Rake tasks to refresh as often as you would like. It really is that simple :)

Upvotes: 0

MikeH
MikeH

Reputation: 866

The Google Sitemap Generator is Google's official release. It's easy to set up and configure.

http://code.google.com/p/googlesitemapgenerator/

Upvotes: 0

John Topley
John Topley

Reputation: 115362

I added a dynamic sitemap to a blog application just recently. These steps should get you started.

Add this route towards the bottom of your config/routes.rb file (more specific routes should be listed above it):

map.sitemap '/sitemap.xml', :controller => 'sitemap'

Create the SitemapController (app/controllers/sitemap_controller):

class SitemapController < ApplicationController
  layout nil

  def index
    headers['Content-Type'] = 'application/xml'
    last_post = Post.last
    if stale?(:etag => last_post, :last_modified => last_post.updated_at.utc)
      respond_to do |format|
        format.xml { @posts = Post.sitemap } # sitemap is a named scope
      end
    end
  end
end

—As you can see, this is for a blog, so is using a Post model.

This is the view template (app/views/sitemap/index.xml.builder):

base_url = "http://#{request.host_with_port}"
xml.instruct! :xml, :version=>'1.0'
xml.tag! 'urlset', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9' do
  for post in @posts do
    xml.tag! 'url' do
      xml.tag! 'loc', "#{base_url}#{post.permalink}"
      xml.tag! 'lastmod', post.last_modified
      xml.tag! 'changefreq', 'monthly'
      xml.tag! 'priority', '0.5'
    end
  end
end

That's it! You can test it by bringing up http://localhost:3000/sitemap.xml (if using Mongrel) in a browser, or perhaps by using cURL.

Note that the controller uses the stale? method to issue a HTTP 304 Not Modified response if there are no new posts sinces the sitemap was last requested.

Upvotes: 2

Rytis Lukoševičius
Rytis Lukoševičius

Reputation: 1288

I really wouldn't recommend using controller and simply routing to it like '/sitemap.xml', because if your sitemaps will grow it will waste resourses. It would be best to make a rake task and regenerate sitemaps every night or so, depending on how frequently the content changes.

If your site does not have anywhere near 50000 pages and you're not planing to have more then you can fit in one sitemap file then I suggest you use "Google Sitemaps with Ruby on Rails, Capistrano, and Cron" I know it works decently because I'm using it sucessfully atm.

Note: I would not suggest using authors way of copying sitemaps every time when deployment task is run, in my opinion it's not clean. It's better to use shared directory and symlink to it on deployment, then you'll be sure to have only one version of files and now wasting space if extra copies get left behind. I have this in my deploy.rb:

desc "Symlink the upload directories"
task :before_symlink do
  run "rm -drf #{release_path}/public/sitemaps"
  run "ln -s #{shared_path}/sitemaps #{release_path}/public/sitemaps"
end

Also you can use "Big sitemap" gem, but I have found it just now and can't tell you how good it is. It looks like it's really easy to setup this gem and it also uses your rails routes for link generation, so you'll have one problem less to solve than with the first solution.

Upvotes: 5

Related Questions