4

Trying to create an Atom feed in Rails 3. When I refresh my browser, I see basic XML, not the Atom feed I'm looking for.

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

localhost:3000/posts.atom looks like this:

<?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>
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
scud bomb
  • 415
  • 3
  • 19

2 Answers2

4

I ran into this same problem.

  1. First make sure the XML that was generated by your .builder file is a valid Atom XML. You can paste it to the W3c feed validator which will tell you if something is wrong with it. I pasted the XML above and there were some issues, it seems. Once you edit .builder file and make the resulting XML pass. Refresh your page with the valid atom feed.

  2. If you still see plain XML, check in your browser's debugger to see what Response headers you get for the feed. Specifically are you getting Content-Type header? The browser needs it to be some xmlish mime type like 'application/xml' or better yet, 'application/atom+xml'. If you're not getting that Content-Type, or getting the wrong one for some reason, you can override the response header from the headers hash directly in the format call in your controller. Simply add a code block with a typical Atom mime type string:

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
yuvilio
  • 3,795
  • 32
  • 35
0

This might help with formatting the feed in XHTML.

Jonathan
  • 10,936
  • 8
  • 64
  • 79