2012-03-27 61 views
3

試圖在Rails 3中創建一個Atom提要。當我刷新瀏覽器時,我看到基本的XML,而不是我正在尋找的Atom提要。Rails 3 Atom Feed

class PostsController < ApplicationController 
    # GET /posts 
    # GET /posts.xml 
    def index 
    @posts = Post.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @posts } 
     format.atom 
    end 
    end 

index.atom.builder

atom_feed do |feed| 
    feed.title "twoconsortium feed" 
    @posts.each do |post| 
    feed.entry(post) do |entry| 
     entry.title post.title 
     entry.content post.text 
    end 
    end 
end 

本地主機:3000/posts.atom看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom"> 
    <id>tag:localhost,2005:/posts</id> 
    <link rel="alternate" type="text/html" href="http://localhost:3000"/> 
    <link rel="self" type="application/atom+xml" href="http://localhost:3000/posts.atom"/> 
    <title>my feed</title> 
    <entry> 
    <id>tag:localhost,2005:Post/1</id> 
    <published>2012-03-27T18:26:13Z</published> 
    <updated>2012-03-27T18:26:13Z</updated> 
    <link rel="alternate" type="text/html" href="http://localhost:3000/posts/1"/> 
    <title>First post</title> 
    <content>good stuff</content> 
    </entry> 
    <entry> 
    <id>tag:localhost,2005:Post/2</id> 
    <published>2012-03-27T19:51:18Z</published> 
    <updated>2012-03-27T19:51:18Z</updated> 
    <link rel="alternate" type="text/html" href="http://localhost:3000/posts/2"/> 
    <title>Second post</title> 
    <content>its that second post type stuff</content> 
    </entry> 
</feed> 
+1

看起來像一個原子飼料給我。也許你只是缺少瀏覽器的閱讀器? – Jonathan 2012-03-28 00:12:57

+0

@defaye這可能是問題,im在鉻,推薦讀者? – 2012-03-28 00:17:53

+1

[shoyu](https://chrome.google.com/webstore/detail/ilicaedjojicckapfpfdoakbehjpfkah)或許 – Jonathan 2012-03-28 00:22:00

回答

3

我遇到了同樣的問題。

  1. 首先確保被你.builder文件生成的XML是一種有效的Atom XML。你可以將它粘貼到W3c feed validator,它會告訴你它是否有問題。我粘貼上面的XML,看起來有些問題。一旦你編輯.builder文件,並讓結果XML通過。使用有效的原子提要刷新頁面。

  2. 如果您仍然看到純XML,請檢查您的瀏覽器的調試器,以查看您獲得的Feed的響應標頭。具體是你越來越內容類型頭?瀏覽器需要它是一些xmlish MIME類型,如'application/xml'或更好,'application/atom + xml'。如果您沒有獲得Content-Type,或者出於某種原因得到錯誤的內容,則可以直接以控制器格式調用覆蓋headers哈希中的響應頭。簡單地用一個典型的原子mime類型字符串添加代碼塊:

respond_to do |format| 
    format.html # index.html.erb 
    format.xml { render :xml => @posts } 
    format.atom { headers["Content-Type"] = 'application/atom+xml; charset=utf-8'} 
end