Create a controller with the name ‘sitemap’ (or) with the name you desire…..
(E.g)
ruby script/generate controller sitemap
(or)
You can manually create one [app/controllers/sitemap_controller.rb]
class SitemapController < ApplicationController
end
Once it’s done, we need to add the functionality to the controller,to generate the urls dynamicallly.
Here, I am forming the urls, based on the fileds from “Category” and “Language” tables in the database. You may need to form different custom url formats. Do it as per your requirement here.
I am doing that through, ‘generate_url()’ function. This method does the following;
1)Retreives all the url fields(categories) from database [@categories = Category.find(:all)]
2)Forms the ‘url format’ [temp_url += "/category/#{slugify(category.name)}"] and adding it to the “Array field” [url.push(temp_url)]
3)The method ‘slugify()’ removes white-spaces, all unnecessary characters and gives you the ‘url supported string’. (e.g) slugify(‘all languages’) will return ‘all-languages’
The final controller code will look like;
class SitemapController < ApplicationController
caches_page :index
def index
headers["Content-Type"] = “text/xml”
@url_collection = generate_url()
enddef generate_url
url = Array.new@categories = Category.find(:all)
@languages = Language.find(:all)
if @categories != nil && @categories != “”
@categories.each do |category|
temp_url = “”
temp_url += “/category/#{slugify(category.name)}”
url.push(temp_url)
end
end
if @languages != nil && @categories != “”
@languages.each do |language|
temp_url = “”
temp_url += “/language/#{slugify(language.name)}”
url.push(temp_url)
end
end
return url
enddef rebuild
expire_page :action => “index”
redirect_to :action => “index”
enddef slugify(text)
return text.downcase.gsub(/&/, ‘ and ‘).gsub(/[^a-z0-9']+/, ‘-’).gsub(/^-|-$|’/, ”)
end
end
The ‘index()’ function here sets the ‘content-type’ to “text/xml” and then calls our ‘generate_url’ function
Also, you may note, ‘caches_page :index’ in the beginning of the line. This is to cache the ‘sitemap’ output, so that the ‘sitemap’ controller is not called every time. The cached output ‘sitemap.xml’ can be found at ‘RAILS_ROOT/public’ folder. When you clear this file(sitemap.xml) in the ‘public’ folder, the sitemap controller will be called again to generate the sitemap again. It’s always the best chocie to cache the sitemap.
To clear the cache and rebuild the sitemap, just invoke;
http://<hostname>/sitemap/rebuild
The next step is to make a ‘view’ to show the output. Make the ‘app/views/sitemap/index.rxml’ file to look like the following one;
xml.instruct!
xml.urlset “xmlns” => “http://www.sitemaps.org/schemas/sitemap/0.9″ do
@url_collection.each do |url|
xml.url do
xml.loc url
xml.lastmod Date.today
xml.changefreq ‘weekly’
xml.priority 0.5
xml.size @url_collection.length
end
end
end
You may also like to add this in your’config/routes.rb’;
map.connect ‘sitemap.xml’, :controller => “sitemap”, :action => “index”
You should see the sitemaps in;
http://<hostname>/sitemap
http://<hostname>/sitemap.xml