Noah Clark
Noah Clark

Reputation: 8131

Building Sitemap in Rails 3.1

I'm trying to follow the first answer here: Google sitemap files for Rails projects

In my routes I have the following:

  match '/sitemap.xml' => "sitemap#index"

In my views (app/views/sitemap/index.xml.erb) I have the following:

<% base_url = "http://#{request.host_with_port}" %>
<% urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"} %>
<% @posts.each do |post| %>
  <url>
    <loc><%= "#{base_url}#{post.permalink}" %></loc>
    <lastmod><%= post.last_modified %></lastmod>
    <priority>0.5</priority>
<% end %>

In my controller I have the following:

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

When I go to http://localhost:3000/sitemap.xml I get a pink/peach colored box that says the following:

This page contains the following errors:

error on line 1 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.

Any idea what this means or where I went wrong?

Added:

Here is the source from the page loading:

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black">
<h3>This page contains the following errors:</h3>
<div style="font-family:monospace;font-size:12px">error on line 1 at column 1: Extra content at the end of the document
</div>
<h3>Below is a rendering of the page up to the first error.</h3>
</parsererror></body></html>

As an additional experiment I added the following:

<?xml version="1.0" ?> 
<note>
<to>Tove</to> 
<from>Jani</from> 
<heading>Reminder</heading> 
<body>Don't forget me this weekend!</body> 
</note>

This creates the same error, but I've tested this and this is valid XML code.

Upvotes: 2

Views: 1260

Answers (1)

Noah Clark
Noah Clark

Reputation: 8131

Here is what I had to do to get it to work:

First in my routes I put the following:

match "/sitemap.xml", :to => "sitemap#index", :defaults => {:format => :xml}

Then I created a Sitemap controller with an index action:

class SitemapController < ApplicationController
  layout nil

  def index
    @posts = Post.all
    @base_url = "http://#{request.host_with_port}"
    headers['Content-Type'] = 'application/xml'
    def index
      respond_to do |format|
        format.html
        format.xml
      end
    end
  end
end

And finally, I created index.xml.erb that looks like this:

<?xml version="1.0" ?> 
<% @posts.each do |post| %>
  <url>
    <loc><%= "#{@base_url}/#{post.slug_url}" %></loc>
    <lastmod><%= post.updated_at %></lastmod>
    <priority>0.5</priority>
  </url>
<% end %>

Upvotes: 3

Related Questions