Generate a Sitemap
Generate a Sitemap XML dinamicly from database items. So, it's automaticly updated on database inserts.
example: sitemap for this site
routes.rb
get '/sitemap', to: 'sitemap#index'
sitemap.controller.rb
class SitemapController < ApplicationController def index render content_type: "application/xml" end end
views/layouts/sitemap.html.erb
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <%= yield %> </urlset>
views/sitemap/index.html.erb
<url> <loc><%= 'https://zguillez.tips' %></loc> <lastmod><%= Date.today.to_s %></lastmod> <changefreq>weekly</changefreq> <priority>1</priority> </url> <% Category.all.each do |cat| %> <url> <loc><%= 'https://zguillez.tips/' + cat.slug %></loc> <lastmod><%= cat.updated_at.strftime("%Y-%m-%d") %></lastmod> <changefreq>monthly</changefreq> <priority>0.8</priority> </url> <% end %> <% Tip.all.each do |tip| %> <url> <loc><%= 'https://zguillez.tips/' + tip.category.slug + '/' + tip.slug %></loc> <lastmod><%= tip.updated_at.strftime("%Y-%m-%d") %></lastmod> <changefreq>yearly</changefreq> <priority>0.5</priority> </url> <% end %>