Rails 3 Atom Feed

4
尝试在Rails 3中创建Atom feed。当我刷新浏览器时,我看到的只是基本的XML,而不是我要寻找的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
看起来像是一个Atom订阅源。也许你的浏览器只是缺少一个阅读器? - Jonathan
@defaye 那可能是问题所在,我在使用Chrome浏览器,你有什么推荐的阅读器吗? - scud bomb
1
shoyu 或许 - Jonathan
2个回答

4

我遇到了同样的问题。

  1. 首先确保由.builder文件生成的XML是有效的Atom XML。您可以将其粘贴到W3c feed validator中,该工具会告诉您是否存在问题。我将上面的XML粘贴到该工具中,发现有一些问题。一旦您编辑了.builder文件并使生成的XML通过验证,请使用有效的Atom feed刷新页面。

  2. 如果您仍然看到纯文本XML,请检查浏览器的调试器以查看您获取的feed的响应头是什么。具体来说,您是否得到Content-Type头?浏览器需要它成为某种类似xml的mime类型,如'application/xml'或更好的'application/atom+xml'。如果您没有获取到Content-Type,或者由于某种原因获取到了错误的Content-Type,则可以在控制器中的格式调用中直接从headers哈希覆盖响应头。只需添加一个典型的Atom 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

0

这个可能有助于在XHTML中格式化提要。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接