Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 22nd, 2012  |  syntax: None  |  size: 2.09 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Building Sitemap in Rails 3.1
  2. match '/sitemap.xml' => "sitemap#index"
  3.        
  4. <% base_url = "http://#{request.host_with_port}" %>
  5. <% urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"} %>
  6. <% @posts.each do |post| %>
  7.   <url>
  8.     <loc><%= "#{base_url}#{post.permalink}" %></loc>
  9.     <lastmod><%= post.last_modified %></lastmod>
  10.     <priority>0.5</priority>
  11. <% end %>
  12.        
  13. class SitemapController < ApplicationController
  14.   layout nil
  15.  
  16.   def index
  17.     headers['Content-Type'] = 'application/xml'
  18.     last_post = Post.last
  19.     if stale?(:etag => last_post, :last_modified => last_post.updated_at.utc)
  20.       respond_to do |format|
  21.         format.xml { @posts = Post.sitemap } # sitemap is a named scope
  22.       end
  23.     end
  24.   end
  25. end
  26.        
  27. This page contains the following errors:
  28.  
  29. error on line 1 at column 1: Extra content at the end of the document
  30. Below is a rendering of the page up to the first error.
  31.        
  32. <html xmlns="http://www.w3.org/1999/xhtml">
  33. <body>
  34. <parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black">
  35. <h3>This page contains the following errors:</h3>
  36. <div style="font-family:monospace;font-size:12px">error on line 1 at column 1: Extra content at the end of the document
  37. </div>
  38. <h3>Below is a rendering of the page up to the first error.</h3>
  39. </parsererror></body></html>
  40.        
  41. <?xml version="1.0" ?>
  42. <note>
  43. <to>Tove</to>
  44. <from>Jani</from>
  45. <heading>Reminder</heading>
  46. <body>Don't forget me this weekend!</body>
  47. </note>
  48.        
  49. match "/sitemap.xml", :to => "sitemap#index", :defaults => {:format => :xml}
  50.        
  51. class SitemapController < ApplicationController
  52.   layout nil
  53.  
  54.   def index
  55.     @posts = Post.all
  56.     @base_url = "http://#{request.host_with_port}"
  57.     headers['Content-Type'] = 'application/xml'
  58.     def index
  59.       respond_to do |format|
  60.         format.html
  61.         format.xml
  62.       end
  63.     end
  64.   end
  65. end
  66.        
  67. <?xml version="1.0" ?>
  68. <% @posts.each do |post| %>
  69.   <url>
  70.     <loc><%= "#{@base_url}/#{post.slug_url}" %></loc>
  71.     <lastmod><%= post.updated_at %></lastmod>
  72.     <priority>0.5</priority>
  73.   </url>
  74. <% end %>