Skip to content

Instantly share code, notes, and snippets.

@lucatironi
Created December 2, 2011 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucatironi/1423556 to your computer and use it in GitHub Desktop.
Save lucatironi/1423556 to your computer and use it in GitHub Desktop.
Sitemap.xml for a Rails App for Google Webmasters Tools
# app/views/sitemap/index.xml.builder
xml.instruct!
xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
xml.url do
xml.loc root_url
xml.priority 1.0
end
xml.url do
xml.loc categories_url
xml.priority 0.9
end
xml.url do
xml.loc about_url
xml.priority 0.9
end
xml.url do
xml.loc blog_url
xml.priority 0.9
end
xml.url do
xml.loc faqs_url
xml.priority 0.9
end
xml.url do
xml.loc contacts_url
xml.priority 0.7
end
xml.url do
xml.loc newsletter_url
xml.priority 0.7
end
xml.url do
xml.loc privacy_policy_url
xml.priority 0.7
end
@categories.each do |category|
xml.url do
xml.loc category_services_url(category)
xml.priority 0.8
end
xml.url do
xml.loc normativa_url(category)
xml.priority 0.7
end
end
@services.each do |service|
xml.url do
xml.loc category_service_url(service.category, service)
xml.priority 0.8
end
xml.url do
xml.loc category_service_buy_url(service.category, service)
xml.priority 0.7
end
end
@articles.each do |article|
xml.url do
xml.loc article_url(article)
xml.lastmod article.updated_at.to_date
xml.priority 0.7
end
end
Article::CATEGORIES.each do |category|
xml.url do
xml.loc articles_url(:cat => category)
xml.priority 0.6
end
end
@faqs.each do |faq|
xml.url do
xml.loc faqs_url(:faq_id => faq.id, :anchor => "faq#{faq.id}")
xml.lastmod faq.updated_at.to_date
xml.priority 0.7
end
end
Faq::CATEGORIES.each do |category|
xml.url do
xml.loc faqs_url(:cat => category)
xml.priority 0.6
end
end
end
MyApp::Application.routes.draw do
# snip...
get '/sitemap', :to => "sitemap#index", :as => "sitemap"
end
class SitemapController < ApplicationController
def index
headers['Content-Type'] = 'application/xml'
@categories = Category.all
@services = Service.published
@articles = Article.published
@faqs = Faq.published
respond_to do |format|
format.xml
end
end
end
require "spec_helper"
describe "SitemapController" do
it "allows google robots to retrieve the site's map" do
get sitemap_path(:format => :xml)
response.should render_template(:index, :format => :xml)
response.body.should include(root_url)
response.body.should include(contacts_url)
response.headers["Content-Type"].should eq("application/xml")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment