2012-05-12 38 views
1

我正在嘗試爲CMS風格的rails應用程序製作一個動態站點地圖,但我在使用Haml在XML中創建站點地圖時遇到問題。我查看了文檔,他們說我應該能夠使用!!! XML在文檔的開頭插入<?xml version="1.0" encoding="UTF-8"?>標籤。當我嘗試這樣做時,它根本不呈現任何東西,我不得不使用字面meta-xml標記。我究竟做錯了什麼?使用Haml創建XML

content_controller.rb 
===================== 
class ContentController < ApplicationController 
    # other methods 

    def sitemap 
    @sections = Section.all :include => :pages 
    respond_to do |format| 
     format.xml 
    end 
    end 
end 

sitemap.xml.haml 
================ 
<?xml version="1.0" encoding="UTF-8"?> 
-# !!! XML 
-# the above tag does not work 
%urlset{:xmlns => 'http://www.sitemaps.org/schemas/sitemap/0.9'} 
    %url 
    %loc= root_url 
    - @sections.each do |section| 
    - section.pages.each do |page| 
     %url 
     %loc= "#{root_url}#{section.url}/#{page.url}" 
     %lastmod= page.updated_at 

回答

4

您需要設置:format => :xhtml才能使其工作。
在你environment.rb

Haml::Template.options[:format] = :xhtml 

更多的信息在這裏http://www.mail-archive.com/[email protected]/msg06984.html

+0

一種選擇是這個配置放在一個正確的初始值,就像在這個[博客文章]建議(http://www.mindgravy.net/2010/10/15 /使用-另一文檔類型與 - HAML和 - 軌道-3 /)。 –