Создание Sitemap в Rails 3.1 - PullRequest
       18

Создание Sitemap в Rails 3.1

1 голос
/ 16 ноября 2011

Я пытаюсь следовать первому ответу здесь: Файлы карты сайта Google для проектов Rails

В моих маршрутах у меня есть следующее:

  match '/sitemap.xml' => "sitemap#index"

В моих представлениях (app / views / sitemap / index.xml.erb) у меня есть следующее:

<% base_url = "http://#{request.host_with_port}" %>
<% urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"} %>
<% @posts.each do |post| %>
  <url>
    <loc><%= "#{base_url}#{post.permalink}" %></loc>
    <lastmod><%= post.last_modified %></lastmod>
    <priority>0.5</priority>
<% end %>

В моем контроллере у меня есть следующее:

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

Когда я перехожу на http://localhost:3000/sitemap.xml, я получаю коробку розового / персикового цвета, на которой написано следующее:

This page contains the following errors:

error on line 1 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.

Есть идеи, что это значит или где я ошибся?

Добавлено:

Вот источник с загрузки страницы:

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black">
<h3>This page contains the following errors:</h3>
<div style="font-family:monospace;font-size:12px">error on line 1 at column 1: Extra content at the end of the document
</div>
<h3>Below is a rendering of the page up to the first error.</h3>
</parsererror></body></html>

В качестве дополнительного эксперимента я добавил следующее:

<?xml version="1.0" ?> 
<note>
<to>Tove</to> 
<from>Jani</from> 
<heading>Reminder</heading> 
<body>Don't forget me this weekend!</body> 
</note>

Это создает ту же ошибку, но я проверял это, и это действительный код XML.

1 Ответ

3 голосов
/ 16 ноября 2011

Вот что мне нужно было сделать, чтобы заставить его работать:

Сначала в моих маршрутах я поставил следующее:

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

Затем я создал контроллер Sitemap с действием index:

class SitemapController < ApplicationController
  layout nil

  def index
    @posts = Post.all
    @base_url = "http://#{request.host_with_port}"
    headers['Content-Type'] = 'application/xml'
    def index
      respond_to do |format|
        format.html
        format.xml
      end
    end
  end
end

И, наконец, я создал index.xml.erb, который выглядит следующим образом:

<?xml version="1.0" ?> 
<% @posts.each do |post| %>
  <url>
    <loc><%= "#{@base_url}/#{post.slug_url}" %></loc>
    <lastmod><%= post.updated_at %></lastmod>
    <priority>0.5</priority>
  </url>
<% end %>
...