Creating an RSS feed in Ruby on Rails

Posted on 22 March 2006

Update (3/10/2010): I've ammended this article to work with new versions of Rails as helpfully pointed out in the comments.

Creating an RSS feed for your website in Ruby on Rails is very simple.

To set it up on this site I added the following lines to my articles controller


def rss
  @articles = Article.find(:all, :order => "id DESC", :limit => 10)
  render :layout => false
  response.headers["Content-Type"] = "application/xml; charset=utf-8"
end

In my views I created an rss.rxml file inside the articles folder and in it contains:


xml.instruct!

xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
 xml.channel do

   xml.title       "paulsturgess.co.uk articles"
   xml.link        url_for :only_path => false, :controller => 'articles'
   xml.description "paulsturgess.co.uk Ruby on Rails articles"

   @articles.each do |article|
     xml.item do
       xml.title       article.title
       xml.link        url_for :only_path => false, :controller => 'articles', :action => 'show', :id => article.id
       xml.description article.content
       xml.guid        url_for :only_path => false, :controller => 'articles', :action => 'show', :id => article.id
     end
   end

 end
end

You can check if your feed validates at feedvalidator.org.

To get browsers to auto discover your rss feed you only need to add one line to your layout template:

<link rel="alternate" type="application/rss+xml" title="RSS" href="url/to/rss/file" />

If you want different views to 'auto detect' different feeds you have created then you can set your path to your rss feed as an instance variable and pass it from the view to the template. I've written another short article on how this works.

About Paul

Paul works for Kyan web design agency in Surrey, UK as a Ruby on Rails developer.

Follow Paul on Twitter

Email: paulsturgess [at] gmail.com

Read more articles in the archive →

Comments...

  • Or you could use the auto_discovery_link_tag helper if you are using 1.2 or Edge.

    Cheers,

    Walter

    Walter McGinnis at 11 Feb 07 at 03:42

  • great article, Paul Sturgess. Its really helpfull

    sunny at 05 Nov 07 at 01:31

  • Really, really helpful! Particularly for someone like me (beginner at RoR).

    Igor at 06 Nov 07 at 03:15

  • That's amazing! This site is so useful!

    Been using ruby for a day now, and overwhelmed by it's awesomeness!

    Richard at 22 Dec 07 at 08:05

  • Switched to ruby after using PHP, rather than going to Cake because I wanted to learn something new, it's all very sensible, particularly easier to create RSS feeds and consume them! Very good tutorial, quick 'n' dirty, many thanks!

    Noirenex at 20 Mar 08 at 14:48

  • This is a SMART STUFF!

    tomomi at 01 Apr 08 at 05:04

  • I tried out the rss thing u explained above and tried to add it to my google home page.
    However It says that the the RSS is not found.
    Do I need to changes anything in the map.resources in the config file ?
    Let me know if any ideas

    Dhaval at 04 Apr 08 at 12:14

  • forgot to mention that the field does not validate in the feedvalidator.org either..

    Dhaval at 04 Apr 08 at 12:15

  • he i tried to add the code but i got some error should i create any database or the xml will create it .

    please i need your help

    shian at 19 Apr 08 at 06:20

  • Thanks a lot for putting this together!

    It seems RSS 2.0 doesn't support relative URL's which is kind of a pain. So

    xml.description article.content

    may not be enough, depending whether it is full HTML or not.

    Stephan

    Stephan Wehner at 12 Jun 08 at 00:25

  • Also came across this one at

    http://feedvalidator.org/docs/warning/MissingAtomSelfLink.html

    bq. If you haven't already done so, declare the Atom namespace at the top of your feed, thus:

    bq. <rss>

    bq. Then insert a atom:link to your feed in the channel section. Below is an example to get you started. Be sure to replace the value of the href attribute with the URL of your feed.

    bq. <atom:link href="http://dallas.example.com/rss.xml" />

    Oh well.

    Stephan

    Stephan Wehner at 12 Jun 08 at 00:35

  • Great article, but i have to do 2 modifications ( i have rails 2.1.0).
    First i have to change :

    render_without_layout

    by

    render :layout => false

    Because render_without_layout it's deprecated in this version of rails (2.1.0)

    and

    @headers["Content-Type"] = "application/xml; charset=utf-8"

    by

    response.headers["Content-Type"] = "application/xml; charset=utf-8"

    and that's it, the rest works fine with the same logic,
    cheers!.

    Alexis at 19 Jun 08 at 15:17

  • Thats cool and all, but how do you make an .rxml file?? in aptana radrails there is no such layout available to me.

    Michael at 16 Jul 08 at 09:56

  • Thats cool and all, but how do you make an .rxml file?? in aptana radrails there is no such layout available to me.

    Michael at 17 Jul 08 at 15:52

  • What should be in the routes file? I have "map.resources :articles", but I get this error

    Couldn't find Article with ID=rss

    newbie at 12 Aug 08 at 17:32

  • Found it...I changed the route to

    map.resources :articles, :collection => {:rss => :get}

    not sure if this is the best way, but it works.

    newbie at 12 Aug 08 at 18:01

  • I already create a table name articles and filleds are title,link,description,guid
    You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.each

    Extracted source (around line #10):

    8: xml.description "paulsturgess.co.uk Ruby on Rails and CSS articles"
    9:
    10: @articles.each do |article|
    11: xml.item do

    bapi at 16 Oct 08 at 05:50

  • Works great, thanks for this guide :) Made setting up rss feeds a cinch (and even customised ones to allow subscribing to categories on my site)

    workmad3 at 01 Nov 08 at 12:25

  • test

    testt at 10 Nov 08 at 11:34

  • Works but how do i use this. It generates an xml file. How will any user subscribe to it? Can someone add more clarity on how exactly to used the produced xml file?

    Sandeep at 03 Jan 09 at 11:57

  • nice example! also check out this one
    http://media.railscasts.com/videos/087_generating_rss.mov

    kazuyoshi tlacaelel at 01 Feb 09 at 00:31

  • I have a requirement to take an RSS feed into a Rails app and automatically update a table from the data provided therefrom (foreign currency exchange rates). I have found the feed tools gem but this seems to be a little old and shows no sign of recent activity. Is there anything else that is current, works with Rails 2.3 and is presently favored by the community?

    poker DVDs at 28 Apr 09 at 07:34

  • hi paul
    ım new on rails
    ı try to create rss feed on my own website.. ı looked your code but ı cant.
    do u give more information to me?

    ercan at 22 May 09 at 03:19

  • hi paul
    ım new on rails
    ı try to create rss feed on my own website.. ı looked your code but ı cant.
    do u give more information to me?

    ercan at 22 May 09 at 03:32

  • This is article i used in early 2007 when i started working on rails. this is really helpful to me that time

    purab kharat at 01 Sep 09 at 08:03

  • Nice Post .. helpful

    Rahul at 31 Mar 10 at 02:13

  • Hi there,

    I changed my "xml.title" and now I get 2 RSS Feeds per item. How can I ged rid of the old ones?

    Any help would be great. Thanks!

    Yasmine at 15 Jun 10 at 15:41

  • Hi there,

    I changed my "xml.title" and now I get 2 RSS Feeds per item. How can I ged rid of the old ones?

    Any help would be great. Thanks!

    Yasmine at 15 Jun 10 at 16:54

  • elegant and very useful
    thanks

    Claudia Farias at 19 Aug 10 at 13:32

  • Thanks for this short and useful tutorial!

    Krani at 13 Sep 10 at 11:40

  • in your rss.rxml, instead of writing ruby code, you can just put your xml elements there such as
    <return>
    <session>akaslf20342034</session>
    </return>

    ridho at 17 Nov 10 at 20:18

  • thanks!

    Andrey at 24 Mar 11 at 01:23

  • Thanks, nice post.

    Thiyagarajan Veluchamy at 17 Jun 11 at 20:07

  • Thanks for posting this, for some reason, none of the other examples I found had the
    response.headers["Content-Type"] = "application/xml; charset=utf-8"

    which was leading me to have <html> then <feed>, and that's not what I wanted.

    john mccaffrey at 02 Sep 11 at 15:31

Got something to say?