Recommended method or plugin for creating Google Sitemaps for rubies in rails app?

I did a quick Google search and did not see anything super-great to automate the creation and updating of my google sitemap for the ruby ​​on rails application. Any suggestions?

+3
source share
4 answers

I recently added a dynamic sitemap to my blogging application. These steps will help you get started.

Add this route to the bottom of your config/routes.rbfile (more specific routes should be listed above):

map.sitemap '/sitemap.xml', :controller => 'sitemap'

Create SitemapController(application / 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 do |format|
        format.xml { @posts = Post.sitemap } # sitemap is a named scope
      end
    end
  end
end

&mdash. , , Post.

(app/views/sitemap/index.xml.builder):

base_url = "http://#{request.host_with_port}"
xml.instruct! :xml, :version=>'1.0'
xml.tag! 'urlset', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9' do
  for post in @posts do
    xml.tag! 'url' do
      xml.tag! 'loc', "#{base_url}#{post.permalink}"
      xml.tag! 'lastmod', post.last_modified
      xml.tag! 'changefreq', 'monthly'
      xml.tag! 'priority', '0.5'
    end
  end
end

! , http://localhost:3000/sitemap.xml ( Mongrel) , , cURL.

, stale? HTTP 304 Not Modified, , Sitemap.

+2

, "/sitemap.xml", , Sitemap , . - Sitemap , , .

50000 , , Sitemap, " Google Sitemaps Ruby on Rails, Capistrano Cron" , , .

.. Sitemap , , -, . , , . deploy.rb:

desc "Symlink the upload directories"
task :before_symlink do
  run "rm -drf #{release_path}/public/sitemaps"
  run "ln -s #{shared_path}/sitemaps #{release_path}/public/sitemaps"
end

" Big sitemap" gem, , , , , , , .

+5
0

I would recommend you check the sitemap_generator gem . It handles all these problems for you ... and really, who wants to bother with creating XML?

Here is an example sitemap to show how you use your Rails models and path helpers to create your sitemap URLs:

# config/sitemap.rb
SitemapGenerator::Sitemap.default_host = "http://www.example.com"
SitemapGenerator::Sitemap.create do
  add '/contact_us'
  Content.find_each do |content|
    add content_path(content), :lastmod => content.updated_at
  end
end

You then use the Rake tasks to update as often as you would like. It's really that simple:)

0
source

Source: https://habr.com/ru/post/1707464/


All Articles