Sitemaps are XML files that provide a map of your URLs to Google and other search engines. They can be especially useful for Rails applications, like a blog, where all the the links may not appear browseable. There are many ways to code a sitemap for a Rails application, but I'll show you a very simple and quick method to get your sitemap up and running in minutes.

Let's assume you already have a working Rails blog. Let's generate a controller for the sitemap along with an index action.

rails g controller Sitemaps index

Now let's work on our view. In the app/views/sitemaps folder which was generated by the Sitemaps controller, change the name of the index file to index.xml.builder . And to the file add the following code.

xml.instruct! :xml, :version => "1.0"
xml.urlset "xmlns" => "" do
  xml.url do
    xml.loc root_url
    xml.priority "1.0"
  @articles.each do |article|
    xml.url do
      xml.loc article_url(article)
      xml.lastmod article.updated_at.to_date
      xml.changefreq "monthly"
      xml.priority "0.5"

As you can see my blog entries are held in the @articles instance variable from my already existing Articles controller. This code will generate the XML content for each article. The urlset element is the document-level Sitemap and the loc element provides the full URL of the article's page. The changefreq element allows the developer to decide which portions of the site need to be crawled more frequently relative to other pages on the site. The other XML elements are somewhat self-explanatory.

Now lets put in a few lines of code into the sitemap controller's index action.

def index
      @articles = Article.all
      respond_to do |format|
          format.xml { render layout: false }

Since the sitemap controller is only going to be responding to XML requests, we can keep the code very simple.

Now for a route.

get     'sitemap' => 'sitemaps#index', :format => 'xml'

Finally, lets tell the bots where to find our sitemap.

Sitemap: /sitemap.xml

You can do a quick test by entering localhost:3000/sitemap.xml into your local server's browser and the sitemap will appear. That's it. A quick and easy sitemap for the web's indexing crawlers