How To Generate RSS Feeds with Rails

May 11, 2006

Making RSS feeds for your Rails applications is one of those things that isn’t documented in very many places, but turns out to be fairly easy to do. Adding RSS feeds to your site is one of the best things you can do to retain a steady visitor base. It gives your visitors an easy way to be notified when you update your site with new content. Here’s the three parts you need to code in order to add an RSS feed.

The Controller Action

In the controller for your site you need to add an action for the feed generation, let’s call ours “feed”.

def feed
  @posts = Post.find(:all, :limit => 10, :order => "created_on DESC")
end

This will get the most recent ten posts out of our database and make them available for us to use in the builder template.

Also, if you are using a layout for your controller you need to not have it render for your feed or else it won’t work correctly. Something like this will fix it:

layout "my_layout", :except => [:feed]

The Builder Template

Builder templates are different from RHTML templates in that they represent a structured XML template instead of an HTML template. They also end in .rxml instead of .rhtml. Here’s the template we want to use that will follow the RSS 2.0 specification. Here’s what our feed.rxml will look like.

xml.instruct! :xml, :version=>"1.0" 
xml.rss(:version=>"2.0"){
  xml.channel{
    xml.title("Your Blog's Title")
    xml.link("http://www.yoursite.tld/")
    xml.description("What your site is all about.")
    xml.language('en-us')
      for post in @posts
        xml.item do
          xml.title(post.title)
          xml.description(post.html_content)      
          xml.author("Your Name Here")               
          xml.pubDate(post.created_on.strftime("%a, %d %b %Y %H:%M:%S %z"))
          xml.link(post.link)
          xml.guid(post.link)
        end
      end
  }
}

If you compare this against the RSS 2.0 Specification you’ll see how it mirrors an RSS 2.0 XML file. The important points to note are that the strftime conversion for the pubDate is necessary, and that I’m using the link for each post as the guid as well since it is fairly safe to assume each post will have it’s own unique link. Your Post object may look slightly different, so make sure you make appropriate changes as necessary to this template. I also make the post description the actual content of the post since I like full-text feeds.

The Auto-Discovery Tag

The last bit to finish off your brand new spankin’ RSS feed is the auto-discovery tag.

<%= auto_discovery_link_tag(:rss, :action => 'feed') %>

Place it within the <head> element of your site’s layout (if you use one), that way your visitors using a browser such as Safari or Firefox will be able to easily see that your site has an RSS feed.

Final Notes

So that’s it for generating an RSS feed in Rails. Fairly easy, huh? The only other thing to really note is that you are free to define how you want to get objects out of your database to add to the feed. Posts only relating to food, just pictures, or whatever. Just expand that :conditions symbol for the find method as needed.