Google sitemap files for Rails projects -
is there easy way create sitemaps file rails projects? dynamic sites (such stack overflow example) there should way dynamically create sitemaps file. way go in ruby and/or rails?
what suggest? there gem out there?
add route towards bottom of config/routes.rb
file (more specific routes should listed above it):
map.sitemap '/sitemap.xml', :controller => 'sitemap'
create sitemapcontroller
(app/controllers/sitemap_controller):
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 |format| format.xml { @posts = post.sitemap } # sitemap named scope end end end end
—as can see, blog, using post
model. haml view template (app/views/sitemap/index.xml.haml):
- base_url = "http://#{request.host_with_port}" !!! xml %urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"} - post in @posts %url %loc #{base_url}#{post.permalink} %lastmod=post.last_modified %changefreq monthly %priority 0.5
that's it! can test bringing http://localhost:3000/sitemap.xml (if using mongrel) in browser, or perhaps using curl.
note controller uses stale?
method issue http 304 not modified response if there no new posts sinces sitemap last requested.
Comments
Post a Comment