- Building Sitemap in Rails 3.1
- match '/sitemap.xml' => "sitemap#index"
- <% 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 %>
- 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
- 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.
- <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>
- <?xml version="1.0" ?>
- <note>
- <to>Tove</to>
- <from>Jani</from>
- <heading>Reminder</heading>
- <body>Don't forget me this weekend!</body>
- </note>
- match "/sitemap.xml", :to => "sitemap#index", :defaults => {:format => :xml}
- 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
- <?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 %>