Creating an RSS feed in Ruby on Rails
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.
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
16 comments made
Or you could use the auto_discovery_link_tag helper if you are using 1.2 or Edge.
Cheers,
Walter
great article, Paul Sturgess. Its really helpfull
Really, really helpful! Particularly for someone like me (beginner at RoR).
That’s amazing! This site is so useful!
Been using ruby for a day now, and overwhelmed by it’s awesomeness!
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!
This is a SMART STUFF!
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
forgot to mention that the field does not validate in the feedvalidator.org either..
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
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
may not be enough, depending whether it is full HTML or not.
Stephan
Also came across this one at
http://feedvalidator.org/docs/warning/MissingAtomSelfLink.html
Oh well.
Stephan
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!.
Thats cool and all, but how do you make an .rxml file?? in aptana radrails there is no such layout available to me.
Thats cool and all, but how do you make an .rxml file?? in aptana radrails there is no such layout available to me.
What should be in the routes file? I have “map.resources :articles”, but I get this error
Couldn’t find Article with ID=rss
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?