Rails 3 Atom Feed

Trying to create an Atom feed in Rails 3. When I refresh my browser, I see the underlying XML, not the Atom 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>

      

+3


source to share


2 answers


I faced this problem.

  • First make sure the XML that was generated by your .builder file is a valid Atom XML. You can paste it into the W3c feed validator , which will tell you that there is something wrong with it. I pasted the XML above and got some problems. Once you've edited the .builder file and run the resulting XML pass. Refresh your page with correct Atom feed.

  • If you still see plain XML, check your browser debugger to see what response headers you get for your feed. Specifically, are you getting the Content-Type header ? The browser needs it to be some xmlish mime type, for example "application / xml" or better yet, "application / atom + xml". If you do not receive this Content-Type or are mistaken for some reason, you can override the response header from the hash headers

    directly in the format call in your controller. Just add a block of code with a typical line like 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

      

+3


source


This can help you format your feed in XHTML.



0


source







All Articles