Sohan's Blog

Living the Developer's Life

Sitemap-generator for Your Rails App and Deployment Solution

When talking about Search Engine Optimization for your public web pages, you probably try to follow the google guidelines for search engine optimization. Its a good one to get started with. For my recent project, www.campzero.com, I read the document and tried to follow the steps. Today, I worked on generating sitemap for www.campzero.com using the following:
  • Installed the site-map generator plugin following this link at Aktagon’s, github link
  • For some reason, probably because of permission issues, my plugin install didn’t generate the sample sitemap.yml file for me. However, I created the config/sitemap.yml file myself and put the following contents there:
domain: www.campzero.com
limit: 5000
priority: 1
change_frequency: weekly
  • Then, I put the call to the sitemap method as shown in the github documentation.
  • Finally, I ran the command rake sitemap:gerenate to generate my sitemap.xml file inside the public folder.
  • So far this went well with the local environment. However, when I deployed this code and ran the rake command in my staging environment, it said it was not finding any model with the sitemap method.
  • After some debugging, I found that the problem was with the code at the file generator.rb where they put model.methods.include?(‘sitemap_options’) to see if the model contains a method named sitemap_options. But, at my staging environment, I found that this was returning false although there was this method inside the model. I changed the code to the following so that the include? check is done both for strings and symbols and it worked fine.
if((model.methods.include?(:sitemap_options) || model.methods.include?('sitemap_options')) && model.sitemap_options != nil)
models << model
end
  • Now, I could generate sitemap at my staging environment.
  • However, soon I found another issue with deployment, as I want my sitemap file to stay between deployments. So, I had to ensure the sitemap file is generated at a shared place other than the default public folder. I did the following changes to the sitemap_generator_task.rake file:
if defined?(SITEMAP_FILE_PATH)
SitemapGenerator::Generator.new(SITEMAP_FILE_PATH).find_models_and_generate
else
SitemapGenerator::Generator.run
end
  • I added the constant SITEMAP_FILE_PATH in my config/environments/staging.rb file to get it working.
  • Finally, I changed my deployment script to define a symlink from the public folder to the shared file path as follows:
run “ln -s #{shared_path}/sitemap/sitemap.xml #{release_path}/public/sitemap.xml”