首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails 3 Atom提要

Rails 3 Atom提要
EN

Stack Overflow用户
提问于 2012-03-28 07:32:30
回答 2查看 1.9K关注 0票数 4

尝试在Rails3中创建Atom提要。当我刷新浏览器时,我看到的是基本的XML,而不是我正在寻找的Atom提要。

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

localhost:3000/posts.atom如下所示:

代码语言:javascript
运行
复制
<?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>
EN

回答 2

Stack Overflow用户

发布于 2012-06-01 06:13:50

我遇到了同样的问题。

Atom

  • 首先要确保由.builder文件生成的是有效的Atom XML。你可以将它粘贴到W3c feed validator,它会告诉你它是否有问题。我粘贴了上面的XML,似乎有一些问题。一旦您编辑了.builder文件并使生成的XML通过。使用有效的atom提要刷新页面。
  1. 如果您仍然看到纯
  2. ,请检查浏览器的调试器,查看您从提要获得的响应头。具体来说,您是否获得了内容类型标头?浏览器需要它是某种xmlish的mime类型,比如'application/xml‘,或者更好的是'application/atom+xml’。如果您没有获得Content-Type,或者由于某种原因而获得了错误的Content-Type,那么可以在控制器的headers调用中直接覆盖来自format散列的响应头。只需添加一个具有典型Atom mime类型字符串的代码块:

代码语言:javascript
运行
复制
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
票数 4
EN

Stack Overflow用户

发布于 2012-03-28 08:27:22

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9899182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档