Ad

How To Render Sitemap.xml In Rails App

- 1 answer

I have added /views/sitemap/index.xml and want it displayed when i go to the relevant url.

class SitemapController < ApplicationController

  def index
    respond_to do |format|
      format.html
      format.xml
    end
  end

end

And in routes.rb

  match "sitemap/" => "sitemap#index"

Using Rails 3

When I go to mydomain.com/sitemap/ I just get a white page. Any ideas?

index.xml

<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

<url>
  <loc>http://www.mydomain.com/</loc>
  <changefreq>weekly</changefreq>
</url>
</urlset>
Ad

Answer

Problem is that you are using your index action to render xml and it will render "index.xml" file not "sitemap.xml" which is what you have created in your views

While your routes are correct, you are using the wrong filename in views

Try renaming sitemap.xml file to index.xml ( in the views/sitemap folder)

If you define name routes, you need to define :format with it

match "/sitemap/sitemap.[:format]", :to => "sitemap#index" 

it will pickup your format from there. Also you can define a default format in the routes

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