views:

60

answers:

2

I'm pretty new to Rails 3, and I'm trying to make an RSS/Atom feed. I know about auto_discovery_link_tag, but what is the associated controller/action supposed to look like?

Thanks!

+1  A: 

Using the auto_discovery_link_tag:

In the controller:

respond_to do |wants|
  wants.html
  wants.atom {render :action=>'index',:layout=>false}
end
jtmkrueger
did this work for you?
jtmkrueger
+3  A: 

@simonista

Auto_discovery_link_tag is a good start. A quick Google search and I found blog posts on How to Create an RSS feed in Rails. Let me fill you in on what your associated controller/action is supposed to look like:

controllers/posts_controller.rb

def feed
    @posts = Post.all(:select => "title, author, id, content, posted_at", :order => "posted_at DESC", :limit => 20) 

    respond_to do |format|
      format.html
      format.rss { render :layout => false } #index.rss.builder
    end
end

By default, this will call index.rss.builder. See, below:

views/posts/index.rss.builder

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
  xml.channel do
    xml.title "Your Blog Title"
    xml.description "A blog about software and chocolate"
    xml.link posts_url

    for post in @posts
      xml.item do
        xml.title post.title
        xml.description post.content
        xml.pubDate post.posted_at.to_s(:rfc822)
        xml.link post_url(post)
        xml.guid post_url(post)
      end
    end
  end
end

This is where all the Railsy magic happens. Here, the RSS feed XML is generated and returned to HTTP.

Good luck @simonista and I hope your blog goes well!

Matt Lennard
Thanks Matt, that makes sense, I had just never heard of the .builder file before. For those who are wondering about Atom, I just did a search for "rails atom.builder" and got a very similar looking code fragment, but for atom. (http://www.papodenerd.net/creating-atom-feeds-with-ruby-on-rails/)
simonista
As much as I wish you chose Atom over RSS, I would recommend that you use only one of these. They represent the same information and most of the modern feed parsing library support both, so they're good to go :) Check this best practices : http://blog.superfeedr.com/Feeds/RSS/Atom/Best%20Practice/feed-publishing-best-practices/
Julien Genestoux
Julien, I agree that people should just pick one and go with it. I just wanted to provide the same information for Atom so that it would be easy for someone to pick either one after reading this page.
simonista
Just wanted to add that you should name your .rss.builder file the same as whatever you name your action (function). So in this case it should be feed.rss.builder rather than index.rss.builder.
simonista