Skip to content

Creating an RSS feed in Ruby on Rails

Posted on 22 March 2006

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")
  render_without_layout
  @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 and CSS 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.

View the snippets archive ››

16 comments made

Walter McGinnis commented on 11 Feb 07 at 03:42

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

Cheers,
Walter

sunny commented on 05 Nov 07 at 01:31

great article, Paul Sturgess. Its really helpfull

Igor commented on 06 Nov 07 at 03:15

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

Richard commented on 22 Dec 07 at 08:05

That’s amazing! This site is so useful!

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

Noirenex commented on 20 Mar 08 at 14:48

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!

tomomi commented on 01 Apr 08 at 05:04

This is a SMART STUFF!

Dhaval commented on 04 Apr 08 at 12:14

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 commented on 04 Apr 08 at 12:15

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

shian commented on 19 Apr 08 at 06:20

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

Stephan Wehner commented on 12 Jun 08 at 00:25

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 commented on 12 Jun 08 at 00:35

Also came across this one at

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

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

<rss>

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.

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

Oh well.

Stephan

Alexis commented on 19 Jun 08 at 15:17

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!.

Michael commented on 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 commented on 17 Jul 08 at 15:52

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

newbie commented on 12 Aug 08 at 17:32

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 commented on 12 Aug 08 at 18:01

Found it…I changed the route to

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

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

Got something to say?

About

Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.

Contact

my name at gmail.com

More snippets

Take a look in the archive

Need a website?

Contact my employer. Make sure to check out our portfolio of work.

Hosting

I recommend hostingrails.com