2012-03-27 65 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